Cannot set player custom properties

Options
Hello, I am trying to save and read a single custom property in my PhotonPlayers, but apparently nothing has worked so far: every time I try to read that property, it returns null. After reading many threads in this forum this is what I got, and it still doesn't work:

This is my code for writing the custom property:
ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable();
hashtable.Add("PlayerStatus", "LoadingLevel");
PhotonNetwork.player.SetCustomProperties(hashtable);


This is my code for reading it:
string status = player.CustomProperties["PlayerStatus"].ToString();

This same code works fine for Room custom properties, but not for PhotonPlayer custom properties. What's wrong here? It's like it never gets updated.
Thanks!

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @Danjel,

    Thank you for choosing Photon!

    You should be joined to a room in order to update player properties!
    Otherwise local player properties could be cached and used when joining or creating a room.

    We always recommend using short string keys and for the player status value I suggest you use an enum (byte or int) instead of string.

    You should have something like this:
    public const string PlayerStatusPropKey = "ps";
    
    void SetStatus(object status)
    {
        ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable();
        hashtable.Add(PlayerStatusPropKey, status);
        PhotonNetwork.player.SetCustomProperties(hashtable);
    }
    
    void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps) 
    {
        PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer;
        Hashtable props = playerAndUpdatedProps[1] as Hashtable;
        //...
        var status = props[PlayerStatusPropKey];
    }