Reliable way of getting player number

Does anyone know of a reliable way to get the player number during, or right before spawn? I am currently using the Photon Player Numbering extensions class, and am using the custom properties to get the 'pNr', but for some reason it changes to 0 or non existent for all clients during spawn.

The reason I want this is so I can use it to spawn different characters based on the player number. I've tried using ActorNumber but that can change depending on if the player disconnects and reconnects, or rejoins the room, etc.

I have created a custom character spawn class that is basically identical to the SingleCharacterSpawnManager class. The only difference is that I have an array of prefabs for the player characters, and want to use the player number to index that array in GetCharacterPrefab.

Here is the code I am currently using to get the player number.

C#:
if (newPlayer.CustomProperties.ContainsKey("pNr"))
{
     int result = Convert.ToInt32(newPlayer.CustomProperties["pNr"]);
     Debug.Log("Character ID: " + result);

     return m_CharArray[result];
}
else
{
      return m_CharArray[0];
}

When the level is loaded, and GetCharacterPrefab is called, the player number is 0 for all connected clients including the master and they all use the same prefab to spawn. After the scene is loaded, the player numbers are correct (0 and 1 for a two player game), but they are already spawned at this point.

Anyone have any ideas?
 
Last edited:
Unfortunately I haven't done this before so if you don't get a response I recommend trying to Photon forums to see if anybody there has any ideas. There are also a few different threads with a multi-character spawn manager which may give you some ideas on how to approach it.
 
Thanks Justin. I found a couple helpful threads over there. The consensus is to always try to use the ActorNumber since this is supposed to be a unique value every time. The problem with this is, every time the player disconnects and reconnects or joins a room, etc the ActorNumber is assigned a new value and always starts at 1. This isn't a good value to use to index an array since with a couple room disconnects, the player could easily be assigned an ActorNumber value larger than lets say, a two element array. I found the PlayerNumbering extension helpful in that it provides a way to have a zero based value depending on the number of players in the room no matter what the ActorNumber value is.

The problem lies in timing. I'm still trying to work it out, but I have it working right now where the prefab that gets used to spawn the player is based off of this player number. Essentially creating a way to have different characters in the scene instead of all the same for every player. I'm still trying to figure out how to pause the Photon message queue to see if that helps with what I am doing because there is an event that doesn't get processed until after the scene is loaded and this event is what is used to trigger the numbering system. I need this to happen before the scene is loaded, and found that Photon recommends pausing the message queue when changing scenes. I haven't been able to figure out how to do this other than PhotonNetwork.IsMessageQueueRunning = false; I'm not sure if this is enough, and need to continue to analyze things.
 
Last edited:
Top