I am developing a multiplayer game with Photon. There is a string variable. I can change this string variable on my master client. I want to sync the string value to all clients, and as long as string value is updated, another function is invoked.
For example, I updated this string variable to “Hello there”, once the clients receives this change, another function is called. I know RPC call can sync the variable like the code below. However, I don’t know how I can invoke another function once the string value changes.
if (PhotonNetwork.isMasterClient)
photonView.RPC (“SendMessageRPC”, PhotonTargets.All, “Hello there!”);
Thanks for your help.
I’m not familiar if Photon has some built in functionality for this (haven’t tried Photon for more than a few hours), but you can pretty easily brute force this.
private string mySyncedString = ""; //This string is somehow synced using Photon, RPC I guess
private string mySyncedStringOld = ""; //old value of the synced string we retain for comparison
void Update()
{
if (mySyncedString != mySyncedStringOld) //Check if mySyncedString has changed
{
callMeWhenStringChanges(); //It changed, so call some function
}
}
void callMeWhenStringChanges()
{
//Do something useful here I assume
//Set the old value of the string to the new value so we can check again in future frames for additional changes
mySyncedStringOld = mySyncedString;
}
CustomProperties are probably the best way to go with this. There is a callback on all clients whenever a custom property value is changed, so you could call your function in that.