- Home /
How to pass variables in a Network using RPC function(s)
This should be an easy question to anyone who tried to make a Multiplayer game;
Please, can anyone tell me in a clear way how to use RPC functions?
From what I understood about it, the RPC function works like this;
using UnityEngine;
using System.Collections;
public class ExampleRPC : MonoBehaviour {
int number = 120; //the number I want to send to another machine using RPC.
public static bool somethingHappens = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(somethingHappens){ //If anything happens that makes this variable true:
networkView.RPC("CallThisFunctionWithRPC", RPCMode.All, number); //Send the variable to anything that has a "CallThisFunctionWithRPC" function(?)
}
}
[RPC] void CallThisFunctionWithRPC(int newNumber){ //The function
//And here? How do I tell in this function to make the "number" of the old machine become the "number" of the new machine?
number = newNumber; //(?)
}
}
I already tried doing this and it didn't work, the variable was 120 all the time even if I changed it ingame, the 2 variables on the 2 different builds were not synchronyzed.
That's because the int "number" is a private int only for ExampleRPC. I'm guessing that when you change it inGame you change a differnt private int in a different class.
The RPC setup is fine.
Yes, actually I succesfully synchronized the 2 variables by adding a few lines of code:
using UnityEngine;
using System.Collections;
public class ExampleRPC : $$anonymous$$onoBehaviour {
int number = 120;
public static bool somethingHappens = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(somethingHappens){
number = 300;
}
if(Network.isServer){
networkView.RPC("CallThisFunctionWithRPC", RPC$$anonymous$$ode.All, number);
}
if(Network.isClient){
CallThisFunctionWithRPC(number);
}
}
[RPC] void CallThisFunctionWithRPC(int newNumber){
number = newNumber;
}
}
$$anonymous$$aybe calling the RPC function every frame is not the best solution, but it works for me, thank you for answering my question even if you didn't really help me.
Your answer
Follow this Question
Related Questions
How to update unity web player 0 Answers
How to correctly send a variable to another function? 1 Answer
How to check my Game System Requirements? 2 Answers
What are the prerequisites of using Unity? 1 Answer
How to make player lose lives? 3 Answers