Assigning a 'role' or player number for each player in the room

Options
Hi guys, I'm still learning PUN2, so this might be something extremely simple that I haven't figured out yet. But I am making a 2v2 game (two bases, with half of each base being controlled by a player) where each player is assigned Player1 or Player2 etc, and then depending on which player he is he controls a certain section of his base. I want to set it up so that every item that the player can interact with does a check for whether that player is Player1 or Player2 before it allows them to interact with it.

How I'm thinking about this is, at the start, the Master Client gets the list of players in the room and then randomly assigns a number to them, and then sends that number via RaiseEvent to the rest of the players in the room so everyone knows which player they are. Is that a sensible way to go about it? Or am I trying to reinvent the wheel here? Thanks for your help.

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @matthewjd24,

    Thank you for choosing Photon!

    Use Player.ActorNumber of PlayerNumbering (see/add "Assets\Photon\PhotonUnityNetworking\UtilityScripts\PhotonPlayer\PlayerNumbering.cs").
  • matthewjd24
    Options
    @JohnTube This is exactly what I was looking for, thank you!

    I was sort of hoping you'd find this post, all of my questions so far have been answered by you in other forum posts :smile:
  • matthewjd24
    edited April 2020
    Options
    Hi @JohnTube I've been tinkering with that script for a few hours now, and I'm having trouble with it always generating -1. I've been using Debug.Log to see if it goes wrong anywhere but it seems it doesn't. Is it possible I'm doing something wrong? I've attached the script below. At line 195, after everything is ran, I try to send the player's number to the console but it's always -1. Thank you for your help.

    https://hastebin.com/ibokisafan.cs

    Edit: Alright, so I've come to the conclusion that I'm trying to access it wrong. I'm trying
    foreach (var item in PhotonNetwork.PlayerList)
            {
                
                Debug.Log(item.GetPlayerNumber());
            }
    
    with
    using Photon.Pun.UtilityScripts;
    
    and I'm going to see how that works out.
    Edit: Alright, that works, except it doesn't tell you which player you are. Reading up on it, you need to pass in the player you want to look up (in this case, yourself). Just trying to figure out how you're supposed to refer to yourself...
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @matthewjd24,

    Local player is PhotonNetwork.LocalPlayer or Player.IsLocal.
  • matthewjd24
    edited April 2020
    Options
    @JohnTube I'm using the below code
    foreach (var item in PhotonNetwork.PlayerList)
            {
                //string myuserID = PhotonNetwork.LocalPlayer.UserId;
    
                int playanumba = item.GetPlayerNumber(item.UserId);
                
                Debug.Log(playanumba.ToString()) ;
            }
    

    since GetPlayerNumber uses
    public static int GetPlayerNumber(this Player player, string userId)
    
    so I believe I'm passing the right information to it. But I am getting an error
    error CS1501: No overload for method 'GetPlayerNumber' takes 1 arguments
    
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Not sure from where you got that from.
    Here is the correct method signature:
    public static int GetPlayerNumber(this Player player)
    
    Just use PhotonNetwork.LocalPlayer.GetPlayerNumber();
  • matthewjd24
    Options
    Thank you, you're a lifesaver.