Pun 2 PlayerList

Im trying to create a playerlist so when a player joins it instantiates a button with his name on it, but all the tutorials are outdated, they dont work in PUN2, can someone help me?

Comments

  • Hi @Avi3ator,

    you would have to handle two different cases. The first one is a client who joins a room. This client can use the OnJoinedRoom callback to instantiate those buttons for himself and all other clients that are already in the room. To access them you can use and iterate through the PhotonNetwork.PlayerList.

    If the client is already in the room and another client joins this room, you can use the OnPlayerEnteredRoom(Player newPlayer) callback. This one gets called whenever another client joins the room. You also would have to cover the case when a client leaves the room. This can be done by using the OnPlayerLeftRoom(Player otherPlayer) callback.
  • Avi3atorAvi3ator
    edited December 4
    @Christian_Simon
    Thank you for you help, but i cant get it to work as intended :/
    The PlayerList only shows the local player name on both buttons?
  • Where do you set the name of each player? Please make sure, that both players have unique names. You can also check this without creating any buttons. Simply use the following code snippet:
    private void OnGUI()
    {
        if (!PhotonNetwork.InRoom)
        {
            return;
        }
    
        GUILayout.BeginVertical();
    
        foreach (Player p in PhotonNetwork.PlayerList)
        {
            GUILayout.Label(p.NickName);
    
            // You can also use this one instead of the above one
            // GUILayout.Label(p.ToStringFull());
        }
    
        GUILayout.EndVertical();
    }
    This has to be in a script attached to a GameObject in the scene.
  • Avi3atorAvi3ator
    edited December 4
    I set it in the start function in my player controller script


    void Start()
    {

    if(PV.IsMine)
    {
    PhotonNetwork.NickName = PlayerInfo.PI.mySelectedCharacter;
    }
    }
  • What is printed on the screen when you add the code snippet from above?
    Please check, what the value of PlayerInfo.PI.mySelectedCharacter is on each client.
  • The Code snippet actually works but i cant get it work when i use the buttons
  • Avi3atorAvi3ator
    edited December 5

    Here is a pic of the 2 players, as you can se in the background the old gui works but the new buttons dont
  • It seems that the Start function of the Player Controller script is called after the OnPlayerEnteredRoom callback. In this case the player's nick name might be empty (as seen on the left picture). It is properly updated on the old GUI because it is repainted every frame.

    If you have the possibility, you can try to set the player's nick name before joining the room. Another option would be, to update the button's text component from time to time. This would require saving a link between the player and "his" button somehow. Since the player's nick name is a Custom Player Property, you can also use the OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps) callback therefore. Again this would require, that you know which button belongs to which client.
  • Okay thank you will try to set the player nickname before joining, i think i know a way of doing that :)
  • Now this happends :open_mouth:



    The host doesnt get the new button when another player joins, and the player 2 doesnt get the right names xd
  • Players that are already in the room behave different compared to players who are joining the room. Please have a look at the following code snippet and try to insert it into your application's logic:
    public class Name : MonoBehaviourPunCallbacks
    {
        public override void OnJoinedRoom()
        {
            foreach (Player p in PhotonNetwork.PlayerList)
            {
                // Add a button for each player in the room.
                // You can use p.NickName to access the player's nick name.
            }
        }
    
        public override void OnPlayerEnteredRoom(Player newPlayer)
        {
            // Add a button for the new player.
            // You can access the new player's nick name by using newPlayer.NickName.
        }
    
        public override void OnPlayerLeftRoom(Player otherPlayer)
        {
            // Remove a button that is related to the player who just left the room.
        }
    }
Sign In or Register to comment.