how to get data from other user

Options
Suppose I have 2 users. On device of user A, I'd like to read some data from user B.

For example, every user has a field named fLevel. The value of the field of user A is 1 and user B's is 2.
I want to get user B's data on user A's device. But I get only 1 no matter what. I even displayed all user's data value on user A's device. it doesn't look like I can't read other user's data on my device.

Can anyone help me? My app goes with PUN, Unity and Android phone. Thanks.

Comments

  • Hi @mhhk88,

    what kind of data is this? If you want to access a value of another client, you would have to synchronize it. You can either do this by using the RaiseEvent function or a RPC if the value is part of an object which has an attached PhotonView component. If the data you want to have is player specific and does not depent on the client's object, you should use the Custom Player Properties. You can set them locally by using PhotonNetwork.player.SetCustomProperties(...);. Once set, those properties are automatically synchronized on all client that are connected to the same. You can access them for example by using PhotonNetwork.otherPlayers[0].CustomProperties[...];.
  • mhhk88
    Options
    The data is the value of a field that attached to a network object (having PhotonView component).

    When joining the game, user A photonNetwork.Instantiate an object with the script which has the field and set 1 to the field. User B does the same job and set 2 to the field when he join the game.
    On the device of user A, I want to read the value of the field of user B which is supposed to be 2.
    But I get 1 no matter what.
    Should I use RPC to change the value of the field after instantiating?
    If so, is the script sentence like this (in case of user B)?
    ...
    public static int level = 2; //the field level should be public & static.
    ...
    PhotonView.RPC("ChangeValue", PhotonTargets.All, level);
    ...
    [PunRPC]
    void ChangeValue(int level) {
    //change the field value with level
    ....
    }

    Is SetCustomProperties useful for me too, in this case?

    Thanks in advance for your help.
  • You can use both options for this scenario. Using the Custom Player Properties would have the advantage, that a client, who joins the game later, receives the up to date value immediately after having joined the room. The disadvantage is, that this value is not directly connected to a certain object, furthermore it is connected to the Player. When you are using RPCs, you can either buffer the message where the value is set (this way a new player gets an up to date value as well) or use the void OnPhotonPlayerConnected(PhotonPlayer newPlayer) callback in order to notify a new player. If you don't change this value very often or at all, you are also fine with using buffered RPCs.