CurrentPlayer and Previous Player

Is it possible to assign a previous player or get the current player's actor number in a room?

As from the documentation you are able to get the next player but I really want to understand if it's possible to assign the previous player or the current player who is playing!

Answers

  • i don't think we get to pick player's actor numbers. you can definitely access the curret player's actor number with PhotonNetwork.LocalPlayer.ActorNumber.

    if you want everyone's number, you can iterate thru PhotonNetwork.PlayerList or PhotonNetwork.PlayerListOthers, if you want to exclude the local player.

    you can assign player nicknames, if you want to keep track of them that way. PhotonNetwork.LocalPlayer.NickName = "Tobias" or whatever :)

  • For me, it's unclear what you want to achieve.

    if it's possible to assign the previous player or the current player who is playing!

    In .. which way? Why?

  • Hey @Tobias, I created a function that sets a custom property for the active player turn using an ID. So basically on awake, I assigned the master client as the first active player that means other players don't get to play unless they are called using the GetNext();

    The problem am facing is that when the game is over it continues to set active next player which I wouldn't want since I want the current player to be the last in the turn when the game is over.

    I used the if-statement to break the cycle if the game is over but it continues to call the next player!

    I hope that is clear. If not here is my code:

    private void Awake()
      {
        Pullthetrigger.onClick.AddListener(TurnBasedButton);
        Pullthetrigger.gameObject.SetActive(false);
    
        playerTurn.text = "START!";
    
        // Store the current room
        room = PhotonNetwork.CurrentRoom;
    
        if (PhotonNetwork.IsMasterClient)
        {
          // As master go active since usually this means you are the first player in this room
    
          // just in case lets set this here once so "OnRoomPropertiesUpdate" is automatically called everywhere
    
          // Get your own ID
          var myId = PhotonNetwork.LocalPlayer.ActorNumber;
    
          // and set it to active
          SetActivePlayer(myId);
        }
        else
        {
          // Otherwise fetch the active player from the room properties
          OnRoomPropertiesUpdate(room.CustomProperties);
        }
      }
    
      // listen to changed room properties - this is always called if someone used "SetCustomProperties" for this room
      // This is basically the RECEIVER and counter part to SetActivePlayer below
      public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged) //lobby
      {
        // Maybe another property was changed but not the one we are interested in
        if (!propertiesThatChanged.TryGetValue(ACTIVE_PLAYER_KEY, out var newActiveID)) return;
    
        // if we got a value but it's not an int something is wrong in general
        if (!(newActiveID is int newActvieIDValue))
        {
          //Debug.LogError("For some reason \"ACTIVE_PLAYER_KEY\" is not an int!?");
          return;
        }
        // otherwise apply the ID
        ApplyActivePlayer(newActvieIDValue);
    
        if (!propertiesThatChanged.ContainsKey(IS_SHOT_KEY)) return;
      }
    
      //onclick 
      public void Turn()
      {
        // this gets the next player after you sorted by the actor number (=> order they joined the room)
        // wraps around at the end
        var nextPlayer = PhotonNetwork.LocalPlayer.GetNext(); //should only happen when isshot is false
    
        // Get the id
        var nextPlayerID = nextPlayer.ActorNumber;
    
        var currentPlayerID = PhotonNetwork.LocalPlayer.ActorNumber;
    
        if(room.CustomProperties.ContainsKey(IS_SHOT_KEY) && (bool)room.CustomProperties[IS_SHOT_KEY]) //this is true
        {
          Debug.Log("Don't move to next player");
          SetActivePlayer(currentPlayerID);
        }
        else //isshot is false
        {
          SetActivePlayer(nextPlayerID);
          Debug.Log("move to next player");
        }
        Debug.Log(PhotonNetwork.CurrentRoom.CustomProperties.ToStringFull());
      }
      // This writes the new active player ID into the room properties
      // You can see this as kind of SENDER since the room properties will be updated for everyone
      private void SetActivePlayer(int id)
      {
        var hash = new ExitGames.Client.Photon.Hashtable();
        hash[ACTIVE_PLAYER_KEY] = id;
        room.SetCustomProperties(hash);
      }
      // this applies all local changes according to the active player
    
      private void ApplyActivePlayer(int id)
      {
        // get the according player
        var activePlayer = PhotonNetwork.LocalPlayer.Get(id);
    
        // Am I this player?
        var iAmActive = PhotonNetwork.LocalPlayer.ActorNumber == id;
    
        Pullthetrigger.gameObject.SetActive(iAmActive);
        // Set the button active/inactive accordingly
    
        // Set the text accordingly
        playerTurn.text = string.Format(iAmActive ? ACTIVE_ME_FORMAT : ACTIVE_OTHER_FORMAT, activePlayer.NickName); //turn off when shot
      }