How to check if the value of CustomProperties equals something?

Options
Hello

I have been cracking my head today against the possibly the simplest thing ever in Photon but I just seem to be unable to get the idea.

Situation:
Two players in a room. They choose a character and set a local bool to true. I add this bool in a hashtable and then I set the customerproperties by .SetCustomProperties. Then I want to instantiate the players' characters ONCE everybody has chosen his character. I am tryign to check this with

foreach (PhotonPlayer photonPlayer in PhotonNetwork.playerList) {
if ((bool)photonPlayer.CustomProperties ["ReadyRace"]) {
}
}
but it throws a Null reference on the IF Statement line. I am pretty sure this is not the right way to check if the bool set is true or false but my question is: How do you check the value of a key in a hashtable?

Comments

  • Oddreams
    Oddreams
    edited June 2017
    Options
    Hello. I think the problem might be with the set of the "ReadyRace" key in the Hashtable, If for some reason, you didnt set the CustomProperties["ReadyRace"] correct, then the error pops up, because it couldnt find anything...

    To be sure, try Debugging the value of the CustomProperties.
    Debug.Log(photonPlayer.CustomProperties["ReadyRace"].toString());
  • HeadlessFly
    edited June 2017
    Options
    Hey @Oddreams ,

    thank you for your reply.

    I made quite a progress with my issue, I seem to be able to set the custom properties correctly and now there is no null reference being thrown.

    However, I have something related to this issue I had previously

    Right now I am trying to do the following thing:
    The two players select a race on the canvas in the beginning of the game. In the beginning I create a hashtable and add a key 'ReadyRace' and a value to false, as well as a key 'Done' and a value to false (the 'Done' will be explained a bit later). Once the player chooses his race, I change the value of 'ReadyRace' to true. Then I perform Update() check for each player in the room to check if this 'ReadyRace' is true and 'Done' is false then change a local int called playersReady to += 1, Also I set the 'Done' value to true and I set the custom properties. In this manner, I want to check when every player in the room has chosen their race to start the game. However, the issue lies in that playersReady is changed to 1 but never changed to 2 even after the two players chose their race.

    Here I have a snipper of my code:
    public bool chosenRace = false; Hashtable playerCustomSettings = new Hashtable (); public int playersReady = 0; // Use this for initialization void Start () { playerCustomSettings.Add ("Ready", chosenRace); playerCustomSettings.Add ("Done", false); PhotonNetwork.player.SetCustomProperties (playerCustomSettings); } // Update is called once per frame void Update () { if (playersReady < 2) { foreach (PhotonPlayer player in PhotonNetwork.playerList) { if ((bool)PhotonNetwork.player.CustomProperties ["Ready"] == true && (bool)PhotonNetwork.player.CustomProperties ["Done"] == false) { playerCustomSettings ["Done"] = true; playersReady += 1; PhotonNetwork.player.SetCustomProperties (playerCustomSettings); Debug.Log (playersReady); } } } } public void ChooseHuman () { humanRace = true; chosenRace = true; playerCustomSettings ["Ready"] = chosenRace; PhotonNetwork.player.SetCustomProperties (playerCustomSettings); chooseRaceCanvas.enabled = false; }


    I tried to Debug Log the playerList and I can see that there are two players in the room but it seems it runs only once the Update like it does not recognize there are 2 players in PhotonNetwork.playerList.

    Do you have any idea what it might cause this issue?

    Thanks!
  • Hi @HeadlessFly
    Did you find the solution?