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

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

matthewjd24
2020-04-20 05:05:36

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
2020-04-20 07:53:29

Hi @matthewjd24,

Thank you for choosing Photon!

Use Player.ActorNumber of PlayerNumbering (see/add "Assets\Photon\PhotonUnityNetworking\UtilityScripts\PhotonPlayer\PlayerNumbering.cs").

matthewjd24
2020-04-20 08:17:23

@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
2020-04-20 10:16:22

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
2020-04-20 10:53:28

Hi @matthewjd24,

Local player is PhotonNetwork.LocalPlayer or Player.IsLocal.

matthewjd24
2020-04-20 11:04:04

@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
2020-04-20 11:19:18

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
2020-04-20 11:33:13

Thank you, you're a lifesaver.

Back to top