Example for Custom 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.

Example for Custom Properties

cschmid996
2017-05-31 10:16:13

Hei Guys,

at first, sry for my bad english, it isn't the best, but i hope you will understand my ask.

I have trouble in understanding the Custom Properties. I don't know whether how to set them up correctly or how to use them later.

I read the Forum already and found a entry about this topic but I didn't understand it until now.. Also i wasn't able to found Tutorials about this topic... I have tried to read the PhotonPlayer.cs itself but I wasnt able to learn in this way..maybe I am to silly to understand that lol :dizzy:

My ask to you is to give me an practise example from beginning. How to set Player custom properties like a color and access it on instantiating the player.
Also i have tried to create a Room custom properties with different maxplayer counts but wasn't successfull.

I know this is a big ask, but I hope someone can help me out there? I found some Exampleprojects in the Asset Store but I was not ready to pay 25$, especially I have no money for this ..

Maybe one of you have a great tutorial for this where it is explained how to set up and use them correctly.

Best regards,
cschmid996

Comments

Elijah
2017-07-02 02:29:57

Hello, here is a tiny example of Custom Properties that I use for my player when getting a kill.

int KillScore = (int)PhotonNetwork.player.customProperties["Kills"];
killScore++;
Hashtable hash = new Hashtable();
hash.Add("Kills", killScore);
PhotonNetwork.player.SetCustomProperties(hash);

Details
First, I made an integer variable that represents the player's kill score (it's automatically 0 if key has never been used before).

int killScore = (int)PhotonNetwork.player.customProperties["Kills"];

The name of the key is within the square brackets(doesn't matter what you name it, as long as you know what it represents).
Also, I CAST it as an integer variable by putting (int) before PhotonNetwork. If you want to cast as a bool var, you would put (bool) and if you'd want a float, you'd put (float) before PhotonNetwork.

Next I would then increment or add on to the variable by however much I need the integer variable to increase by, for example :

killScore++;
(or) killScore = killScore + 1;

After we have updated the score by however much we need, we would then save or set that value to our individual photonplayer to be synced all across the room for anyone else to examine. Here's how :

Create a Hashtable variable :

Hashtable hash = new Hashtable();

Then, you would set that Hashtable with the newly updated integer value(our new killScore):

//same "key" from earlier is ("Kills")
hash.Add("Kills", killScore);

Once that's done, you will save that info to our photon player :

PhotonNetwork.player.SetCustomProperties(hash);

IMPORTANT TIP
For any script that uses the Hashtable variable, You MUST include the following line. Make sure to put it at the top of your script!! (C#)

using Hashtable = ExitGames.Client.Photon.Hashtable;

There we go, now our player's killscore has been updated across the network!

MOTYSHIZ
2019-10-28 05:06:02

This example really should be added to the official documentation.

I wouldn't have figured it out nearly as easily without you explaining it like this, so thank you so much!

@Elijah wrote:

Hello, here is a tiny example of Custom Properties that I use for my player when getting a kill.

int KillScore = (int)PhotonNetwork.player.customProperties["Kills"];
killScore++;
Hashtable hash = new Hashtable();
hash.Add("Kills", killScore);
PhotonNetwork.player.SetCustomProperties(hash);

Details
First, I made an integer variable that represents the player's kill score (it's automatically 0 if key has never been used before).

int killScore = (int)PhotonNetwork.player.customProperties["Kills"];

The name of the key is within the square brackets(doesn't matter what you name it, as long as you know what it represents).
Also, I CAST it as an integer variable by putting (int) before PhotonNetwork. If you want to cast as a bool var, you would put (bool) and if you'd want a float, you'd put (float) before PhotonNetwork.

Next I would then increment or add on to the variable by however much I need the integer variable to increase by, for example :

killScore++;
(or) killScore = killScore + 1;

After we have updated the score by however much we need, we would then save or set that value to our individual photonplayer to be synced all across the room for anyone else to examine. Here's how :

Create a Hashtable variable :

Hashtable hash = new Hashtable();

Then, you would set that Hashtable with the newly updated integer value(our new killScore):

//same "key" from earlier is ("Kills")
hash.Add("Kills", killScore);

Once that's done, you will save that info to our photon player :

PhotonNetwork.player.SetCustomProperties(hash);

IMPORTANT TIP
For any script that uses the Hashtable variable, You MUST include the following line. Make sure to put it at the top of your script!! (C#)

using Hashtable = ExitGames.Client.Photon.Hashtable;

There we go, now our player's killscore has been updated across the network!

Eristen
2020-12-26 20:34:03

Awesome! Thanks Elijah! Making a little game of mine and this helps a lot. Sweet. Merry Christmas!

Nafis53
2021-05-11 14:18:55

Thanks guys,It was really helpful.Just one more doubt.Are we supposed to make separate hashtables for each player properties.For example,if I had more attributes of players,like score,assists etc,do I have to make more hashtables for them and set them up according to the way you just said?

cschmid996
2021-05-11 14:53:06

Hei Elijah,
thank you very much for your awesome answer! Sadly a lot happened since 2017 and I didn't saw your answer earlier. But thank you very much, the guide and explanations are great and very helpfull!

I wish you all the best and happy coding!

Best regards,
Chris

Peteroid
2022-02-09 22:48:31

if my experience is valid (i.e. i am not doing something wrong...hehe)... when one sets the custom properties via SetCustomProperties... the change does NOT take effect immediately... so one must wait before trying to get properties via CustomProperties[property_key],, though i have not found any callback like OnCustomePropertiesChanged()...

.

i also don't know if this delay is only when first adding the property to the CustomProperties...

JohnTube
2022-02-11 15:34:07

Hi @Peteroid,

Thank you for choosing Photon!

Did you read this?

322ita
2022-04-16 12:41:34

hi i'm getting an error https://forum.photonengine.com/discussion/20057/object-reference-not-set-to-an-instance-of-an-object-on-custom-properties#latest

does anyone why it does it?

thanks

Back to top