MWsan
MWsan

Reputation: 457

Photon Network Player.SetCustomProperties not working

I am currently building an online game with Photon Networking using Unity and I have a problem with Photon.Realtime.Player.SetCustomProperties() method. I've already googled it but I couldn't find anything like my issue.

About the project

The matchmaking system is nothing very fancy: I have a first scene to login (without password for now), that will connect to Photon. When OnConnectedToMaster event is raised I load the lobby scene that will display the rooms. And finally, when OnJoinedRoom is called I load a 3rd scene for the room itself, it will display the players, set teams, game config, etc.

I've made an extension class both for Player and Room (RoomInfo) classes to make it easier and clean to get/set the Custom Properties.

The issue

As soon as I load the Room scene I want to get a few properties from the Player to display in the Room, like MMR (ranking), for example. So I made the following code:

public static class PlayerExtensions
{
    private static readonly string _mmrProperty = "mmr";

    public static void SetMmr(this Player player, int mmr)
    {
        player.SetCustomProperties(new Hashtable() { { _mmrProperty, mmr.ToString() } });
    }

    public static int GetMmr(this Player player)
    {
        return (int)player.CustomProperties[_mmrProperty];
    }
}

After getting null exception error on GetMmr() I realized the player doesn't have the actual 'mmr' Custom Property in it. So I debugged the SetMmr() and I noticed a few things:

This is me debugging it on 3 steps: https://i.sstatic.net/rMyzR.jpg

So, I am not sure what to do next since the SetCustomProperties() doesn't seem to work. Any ideas? Thanks.

Upvotes: 0

Views: 3656

Answers (1)

JohnTube
JohnTube

Reputation: 1812

tl;dr

  • never set Player.CustomProperties or Room.CustomProperties directly (use them as read-only), instead always use SetCustomProperties methods.
  • wait for OnPlayerPropertiesUpdate callback before trying to access updated property value you just set.

By default, setting properties for actor or room properties will not take effect on the sender/setter client (actor that sets the properties) immediately when joined to an online room unlike what it used to be in PUN Classic. Now, instead, the sender/setter client (actor that sets the properties) will wait for the server event PropertiesChanged to apply/set changes locally. So you need to wait until OnPlayerPropertiesUpdate or OnRoomPropertiesUpdate callbacks are triggered for the local client in order to access them. The new behaviour is due to the introduction of the new room option flag roomOptions.BroadcastPropsChangeToAll which is set to true by default. The reason behind this is that properties can easily go out of synchronization if we set them locally first and then send the request to do so on the server and for other actors in the room. The latter might fail and we may end up with properties of the sender/setter client (actor that sets the properties) different locally from what's on the server or on other clients. If you want to have the old behaviour (set properties locally before sending the request to the server to synchronize them) set roomOptions.BroadcastPropsChangeToAll to false before creating rooms. But we highly recommend against doing this.

source

Upvotes: 3

Related Questions