How to read player's custom properties?

Options
Hello!

I am doing something... but I don't know if I'm doing it right.

Well, the player's prefab has an script called CharacterData, this script loads all the character's data from my DB.

To read this information from other scripts, I would like to use custom properties. For this, I am using:

void Start()
{
getData(); (Gets the user data from the DB)
photonView.owner.customProperties.Add("HP", charHP); // Adds the user's HP as a custom property
photonView.owner.customProperties.Add("CURR_HP", char_currHP); // Adds the user's current HP as a custom property
}

but I don't know how to read the custom property. How could I get it and read its value?

For example, in another script (it's a raycast):

float targetHP = target.collider.GetComponent<PhotonView>().owner.customProperties("HP"); // Supposed to get the custom property HP?

So, how could I do that?

Comments

  • Rellfy
    Options
    Hello. I already fixed it, after 3 hours trying...

    That's an example of how you get it:

    GetComponent<PhotonView>().owner.customProperties["property"];

    in my case, this would be:

    Convert.ToInt32(GetComponent<PhotonView>().owner.customProperties["HP"]);

    (because I want it to be INT/Float, and the property is called HP).
  • Tobias
    Options
    Thanks for updating us. Sorry you had to find it yourself.
    The value of the custom property is of the Type you store, so you don't have to Convert. You can use a simpler cast.
  • Rellfy
    Options
    Hello, thanks for the reply.

    However, when I try to use the custom property in an if, it says the custom property is an object, and not an integer for example.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited July 2015
    Options
    I think what Tobias meant by a simple cast is :
    int hp = (int)(GetComponent<PhotonView>().owner.customProperties["HP"]);