How to sync a variable through Photon

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to sync a variable through Photon

SebGM2018
2018-11-22 20:35:25

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();
PV.RPC("Setting", RPCTargets.All);
But the int value is null. What am I doing wrong?

Comments

OneManArmy
2018-11-22 23:02:33

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).

SebGM2018
2018-11-23 03:24:07

@OneManArmy wrote:

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).

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
2018-11-23 11:28:31

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.

Back to top