Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Third Party Update player button using photon

Discussion in 'Multiplayer' started by tenzolinho, Jan 31, 2021.

  1. tenzolinho

    tenzolinho

    Joined:
    Jul 26, 2019
    Posts:
    6
    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`.

    Code (CSharp):
    1.     private PassTurn()
    2.     {
    3.         PunHashtable _myCustomProperties_forCurrentPlayer = new PunHashtable();
    4.         _myCustomProperties_forCurrentPlayer["Turn"] = false;
    5.         PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties_forCurrentPlayer);
    6.  
    7.         foreach (Player player in PhotonNetwork.PlayerList)
    8.         {
    9.             // ... some logic to determine the next player to set the 'Turn' property to true
    10.  
    11.             if (someCondition for player) {
    12.                 PunHashtable _myCustomProperties_forNextPlayer = new PunHashtable();
    13.                 _myCustomProperties_forNextPlayer["Turn"] = true;
    14.                 player.SetCustomProperties(_myCustomProperties_forNextPlayer);
    15.             }
    16.         }
    17.     }
    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).

    Code (CSharp):
    1. public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
    2.         {
    3.             base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);
    4.  
    5.             if (targetPlayer != null) {
    6.                 if (changedProps.ContainsKey("Turn"))
    7.                 {
    8.                     {
    9.                         foreach (Player player in PhotonNetwork.PlayerList)
    10.                         {
    11.                             print(player.ActorNumber + " " + player.CustomProperties["Turn"]); // this will be 1 False, 2 False
    12.                             if ((bool)player.CustomProperties["Turn"] == true)
    13.                             {
    14.                                 print("HEY HEY HEY 1");
    15.                                 endTurnBtn.SetActive(true);
    16.                             }
    17.  
    18.                             if ((bool)player.CustomProperties["Turn"] == false)
    19.                             {
    20.                                 print("HEY HEY HEY 2");
    21.                                 endTurnBtn.SetActive(false);
    22.                             }
    23.                         }
    24.                     }
    25.                 }
    26.             }
    27.         }

    This is not working properly since it will always enter on `HEY HEY HEY 2.`

    Which is the right approach for this situation? Why isn't `OnPlayerPropertiesUpdate` called twice? Thank you for your time!
     
    Last edited: Jan 31, 2021
  2. tenzolinho

    tenzolinho

    Joined:
    Jul 26, 2019
    Posts:
    6
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    If it's a problem that properties don't change immediately locally, there is a setting you can set at room creation:
    In RoomOptions, set BroadcastPropsChangeToAll to false.
    This allows the client to update the value locally immediately. However, this might cause de-syncs, as the clients don't get the updated values (only a problem when multiple clients set the same key in the same properties (that of a specific player or the room)).