How to use custom player properties

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to use custom player properties

timsk
2012-04-15 09:26:40

I noticed in the last update that PhotonPlayer.setCustomProperties() was added. I was wondering how one would use custom properties for players? Could they be used to say... add a bool "ready" that can then be checked up other players?

I'm also not sure how to actually set custom properties for players, the only way I can get access to the method is by creating:

foreach(PhotonPlayer player in PhotonNetwork.playerList)

Then using player.get a dropdown of PhotonPlayer methods here. I find it strange that I cannot do PhotonPlayer. to access the methods of that class.

Hope someone could shed some light on this, if photon was more documented I wouldn't be asking ;)

Comments

dreamora
2012-04-15 13:17:12

You should post PUN (photon unity network) related questions in the Photon Unity Network board, yields faster and better answers than when you post them on the unrelated DotNet one :)

as for the question itself: you can access the properties on the player itself, they are exposed as a dictionary<,> if I remember right. The same goes for rooms which can have properties too

timsk
2012-04-15 14:39:14

Ah, I wasn't sure which forum to use as this dotnet one has "unity3d" in the subtitle.

I won't double post it over there, but if a mod could move it, that would be great.

I understand I can expose and cache the player properties, it is how to set and use custom properties I am having trouble with.

Firstly, how would I actually set player properties? Am I right in creating a forloop that sets them, or should I use a buffered RPC ?

Secondly, can I add a bool to the properties, and use it "like a variable" ? As in, can I change it locally, then get others to check the value of it?

Pseudo:

setCustomProperties(bool ready)

CheckProperties()
{
    foreach(playerproperties in playerpropertieslist)
    {
        if(ready)
            display ready GUI
    }
}

I hope this describes my issue better. Thanks.

Tobias
2012-04-16 08:23:12

Moved the topic.

You can set custom properties as Hashtable with string keys and any (supported) type as value. So your ready-field could be set as: Hashtable someCustomPropertiesToSet = new Hashtable() {{"rdy", ready}}; PhotonNetwork.player.SetCustomProperties(someCustomPropertiesToSet);

SetCustomProperties actually leaves alone any properties already existing. It's more like a "UpdateCustomProperties".

timsk
2012-04-16 09:24:18

Thanks for moving the post.

Again, I think my question is being misconstrued. My issue isn't with how to call methods and assign properties, it's how to use the properties in a practical example.

Is it possible to create a "ready" key, and assign the value "true" or "false" to it (it has to be a string, right?) locally.

If that is possible, can I then easily check the value of the "ready" key from other players using string.Contains().

If this is not possible, what are custom player properties designed to be used for?

Tobias
2012-04-17 17:03:09

Hm. I hope I now give a useful answer. Else, I'm not getting the question again :) I just wrote better support for player properties in PUN. It will be in the next update which is in the store ... asap.

/// &lt;summary&gt;
/// Sets this (local) player's properties.
/// This caches the properties in PhotonNetwork.player.customProperties.
/// CreateRoom, JoinRoom and JoinRandomRoom will all apply your player's custom properties when you enter the room.
/// While in a room, your properties are synced with the other players.
/// If the Hashtable is null, the custom properties will be cleared.
/// Custom properties are never cleared automatically, so they carry over to the next room, if you don't change them.
/// &lt;/summary&gt;
/// &lt;remarks&gt;
/// Don't set properties by modifying PhotonNetwork.player.customProperties!
/// &lt;/remarks&gt;
/// &lt;param name="customProperties"&gt;Only string-typed keys will be used from this hashtable. If null, custom properties are all deleted.&lt;/param&gt;
SetPlayerCustomProperties(Hashtable customProperties)

This lets you do exactly what you asked for: set a property for your player, it gets synced and when that's done, you can check it with customProperties.Contains or TryGetValue(). The value can be non-string but you have to cast to the type you expect.

timsk
2012-04-17 17:26:25

Perfect, thanks a lot. It's great that you've updated the description too, I hope theres a little hall of fame in the office with my name on it "Timsk: Pointed out the issues with custom properties" 8-) .

Tobias
2012-04-18 14:11:23

Hehe. You are stalking out office?! ;)

The new version is in the Asset Store: http://u3d.as/content/exit-games/photon ... orking/2ey

timsk
2012-04-21 20:47:29

Sorry to wake this thread up again but, given the new description, I was wondering why the following code snippet doesn't work:

			foreach(PhotonPlayer player in PhotonNetwork.playerList)
			{
				GUILayout.BeginHorizontal();
				GUILayout.Label(" " +player.name);
				if((bool)player.customProperties&#91;"Ready"&#93; == true)
				{
					GUILayout.Label("I'm Ready!");
				}
				else if((bool)player.customProperties&#91;"Ready"&#93; == false)
				{
					GUILayout.Label("Hold on a tick!");
				}
                                GUILayout.EndHorizontal();
                        }

The strange thing is, it works for the local player, but other players just stay on "Hold on a tick!". Through debugging, it appears the custom properties are only synced when a player joins a room, and are never sent again. Yet you state: "While in a room, your properties are synced with the other players." which led me to believe that they are synced while in a room, as in, not just when you join.

Could you clarify please?

Ta.

P.S. I've tried .ContainsValue(true); as well, same results.

dreamora
2012-04-21 20:59:24

The custom properties should broadcast but you need to tell them to do so when setting a property. Potentially the PUN layer doesn't do that (not sure, haven't checked the latest version. the one we use doesn't expose custom props at all) in which case the update is only sent from client to photon but not broadcasted to the room itself and the other clients which would result in what you see

timsk
2012-04-22 10:53:25

Should've paid more attention to "Don't set properties by modifying PhotonNetwork.player.customProperties!". Now that I'm using SetCustomProperties to set them, all is well :).

Tobias
2012-04-23 15:15:59

Good to read. Sorry for that misleading property. Maybe we can hide it better in the upcoming implementations.

agarrote
2016-05-10 22:45:18

dreamora, or anybody, I think Im having this problem:

I have 2 players, masterClient setCustom properties for himself on OnCreatedRoom () and for others on OnPhotonPlayerConnected( PhotonPlayer newPlayer ) , now, I can get the properties values using PhotonNetwork.player.customProperties["key"] but this only seams to work on masterClient, other players cant get the values.
How can I broadcast customProperties ? to other clients

vadim
2016-05-13 07:43:16

What exact call do you use when setting player's properties? Please also log PhotonNetwork.connectionStateDetailed when setting and getting properties to make sure that client is joined to a room.

Back to top