Example for Custom Properties

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


Answers

  • 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!
Sign In or Register to comment.