Search code examples
unity-game-enginemultiplayerphoton

Update player button photon


I have this scenario: The first user that joins a lobby will have an active button on his screen. After some actions, if the player hit the button, it will disable, and then the next player in the room will have this button active, and so on. Based on a property 'Turn' I want to change this button state like this.

In this function PassTurn I first set for the LocalPlayer the Turn property to false and then I determine the next player using some logic, and set for that player the Turn property to true.

private PassTurn()
{
    PunHashtable _myCustomProperties_forCurrentPlayer = new PunHashtable();
    _myCustomProperties_forCurrentPlayer["Turn"] = false;
    PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties_forCurrentPlayer);

    foreach (Player player in PhotonNetwork.PlayerList)
    {
        // ... some logic to determine the next player to set the 'Turn' property to true

        if (someCondition for player) {
            PunHashtable _myCustomProperties_forNextPlayer = new PunHashtable();
            _myCustomProperties_forNextPlayer["Turn"] = true;
            player.SetCustomProperties(_myCustomProperties_forNextPlayer);
        }
    }
}

In OnPlayerPropertiesUpdate function, if Turn property ever change, activate or deactivate the button based on Turn property value (if true - show the button, if false don't display the button).

public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
    {
        base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);

        if (targetPlayer != null) {
            if (changedProps.ContainsKey("Turn"))
            {
                {
                    foreach (Player player in PhotonNetwork.PlayerList)
                    {

                        // this print will display (1, false) and (2, false) which is not correct, because the second player doesn't have de property changed
                        print(player.ActorNumber + player.CustomProperties["Turn"];

                        if ((bool)player.CustomProperties["Turn"] == true)
                        {
                            print("HEY HEY HEY 1");
                            endTurnBtn.SetActive(true);
                        }

                        if ((bool)player.CustomProperties["Turn"] == false)
                        {
                            print("HEY HEY HEY 2");
                            endTurnBtn.SetActive(false);
                        }
                    }
                }
            }
        }
    }

This is not working properly since it will always enter on HEY HEY HEY 2., the button for the current play will disappear, but it will never appear on the next player.

Which is the right approach for this situation? How can I change ui components on other players screen? Thank you for your time!

LaterEdit based on accepted comment:

public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
{
    base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);

    if (targetPlayer != null) {
        if (changedProps.ContainsKey("Turn"))
        {
            {
                    if ((bool)PhotonNetwork.LocalPlayer.CustomProperties["Turn"] == true)
                    {
                        print("HEY HEY HEY 1");
                        endTurnBtn.SetActive(true);
                    }

                    if ((bool)PhotonNetwork.LocalPlayer.CustomProperties["Turn"] == false)
                    {
                        print("HEY HEY HEY 2");
                        endTurnBtn.SetActive(false);
                    }
            }
        }
    }
}

Solution

  • you iterate over every player and check the variable, so as soon as the last player in your list has the turn property false every player will disable the button, while when the last players turn property is true every player will activate the button. Either way it makes no sense to iterate over every player in the room, since you only want to change the locally shown button. Therefore you should only check the local player/players and change the button accordingly.