How to set a local variable by means of an RPC function? (PUN2)

Options
Hi to everyone,
I'm using the PUN2 framework. Is there a way to change the value of a variable in a client (or in all the clients) script from another client by means of an RPC function? For example, let's suppose that an integer variable "intvalue" and an RPC function "RPC_ChageValue(int changeVal)" are defined in a C# script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;


public class PUN2_Class : MonoBehaviourPun
{
public int intvalue;

void Start()
{
intvalue = 0;
}


void Update()
{
if(photonView.IsMine)
{
if (Input.GetKeyUp("a"))
{
photonView.RPC("RPC_ChageValue()",RpcTarget.AllBufferedViaServer,Random.Range(0,10));
}
{

Debug.Log("intvalue in Update: " + intvalue);
}


[PunRPC]
RPC_ChageValue(int changeVal)
{
intvalue = changeVal;
Debug.Log("intvalue: in the RPC function " + intvalue);
}

}

the "RPC_ChageValue(int changeVal)" function is called when a client push the "a" button, but the value of the variable "intvalue" changes only in the client which calls the RPC function. In case of a client different from me calls the RPC function, If I debug "intvalue" in the RPC funtion I will see the updated value of "intvalue", but if I debug it in the "Update" function the variable hasn't the new value. It seems that it is not possible to store a value passed by means of an RPC funtion in a local variable as the "intvalue" one of the example. It is right? Is there a solution to this? For example, It is possibile to store the value in a variable which is defined in the server in order to a client can access to that variable during a match? Thank you!

Comments

  • jeanfabre
    Options
    Hi,

    use Room custom properties for this, simply set the room custom property and other clients ( or logic scripts) simply listen to the room property change callbacks, you don't need RPC for this.

    Bye,

    Jean