Photon Networking Unity AllProperties not setting

759 views Asked by At

I started using Photon Networking for Unity and I ran into a problem. I want to add to the CustomProperties in the player and then I want to debug the result. However the debug prints "Null". I do this after the room is created.

The funny thing is in the OnPhotonPlayerPropertiesChanged() it does print "changed" and only does that when I execute SetPlayerPosition().

But if I then check for the key inside the customproperties is doesn't contain it so it does not print "10"?

    void Awake()
    {
        SetPlayerPosition();
    }

    public override void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
    {
        Debug.Log("changed");
        if (PhotonNetwork.player.CustomProperties.ContainsKey("1"))
        {
            Debug.Log("it works");
        }
    }

    void SetPlayerPosition()
    {
        ExitGames.Client.Photon.Hashtable xyzPos = new ExitGames.Client.Photon.Hashtable();
        xyzPos.Add(1, "10");
        xyzPos.Add(2, "5");
        xyzPos.Add(3, "10");
        PhotonNetwork.player.SetCustomProperties(xyzPos);
        // PhotonNetwork.player.SetCustomProperties(xyzPos, null, true); doesnt work either
    }
2

There are 2 answers

0
anonymous-dev On BEST ANSWER

Actually the awnser is that the keys need to be strings!

0
JohnTube On

According to PUN's doc-api you should do this:

void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps) 
{
    PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer;
    Hashtable props = playerAndUpdatedProps[1] as Hashtable;

    Debug.Log(string.Format("Properties {0} updated for player {1}", SupportClass.DictionaryToString(props), player);
    if (player.CustomProperties.ContainsKey("1"))
    {
        Debug.Log("it works 1");
    }
    if (props.ContainsKey("1"))
    {
        Debug.Log("it works 2");
    }
}