How to sync a variable through Photon
Options
I need to sync a variable so all the players have the same value. I have tried with something like this
[PunRPC]
void Setting ()
{
I = somevalue;
//I Is my int
}
And I call it the following way:
PhotonView PV = GetComponent<PhotonView>();
PV.RPC("Setting", RPCTargets.All);
But the int value is null. What am I doing wrong?
[PunRPC]
void Setting ()
{
I = somevalue;
//I Is my int
}
And I call it the following way:
PhotonView PV = GetComponent<PhotonView>();
PV.RPC("Setting", RPCTargets.All);
But the int value is null. What am I doing wrong?
0
Answers
-
public float someFloat = 10f;
public int someInt = 100;
public PhotonView pv;
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
pv.RPC("SyncValues", RPCTarget.OthersBuffered, someFloat, someInt);
}
}
[PunRPC]
void SyncValues (float myFloat, int myInt){
someFloat = myFloat;
someInt = myInt;
}
//Buffered - New players get the RPC when they join as it's buffered (until this client leaves).0 -
And if I want to sync this var to all the players (including me), in this precise moment?, like, at the moment i press a button, Is there any change?OneManArmy said:public float someFloat = 10f;
public int someInt = 100;
public PhotonView pv;
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
pv.RPC("SyncValues", RPCTarget.OthersBuffered, someFloat, someInt);
}
}
[PunRPC]
void SyncValues (float myFloat, int myInt){
someFloat = myFloat;
someInt = myInt;
}
//Buffered - New players get the RPC when they join as it's buffered (until this client leaves).
0 -
RPCTarget.All, or RPCTarget.AllBuffered.
This is just an example.
To see changes add some UI and check if (pv.isMine) before you send RPC.0