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! Your search result can be found below. Plus, we offer support via 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.

PlayerList.ActorNumber problem

Axel
2019-09-24 08:24:56

Good morning everyone,

I'm trying to create a multiplayer application for unity with PUN2.

This application is simple, first, you are into the lobby room and you create or join an existing room. Next, you join a waiting room with all the other players in this room.

My problem is this: Each player have a specified spawn point and this spawn point is attached to the Actor number of the player. For instance, for 4 players into the room, Player1 spawns at the spawn1, Player2 spawns at the spawn2 etc... If one player leaves the room, I want the next player to spawn at the position of the previous player. For instance, if 1,2,3 and 4 are present into the room and 2 leaves, I want the new player who spawns into the waiting room to becomes the player2 and not the player5.

I used the following property "PhotonNetwork.PlayerList[i].ActorNumber" in order to recover the actor number of the player. However, when I use this code I obtain [1,3,4,5] into the room instead of [1,2,3,4] when a player leave the room.

Does someone have an idea for helping me, please?

Best regards,
Axel

Comments

JohnTube
2019-09-24 10:32:29

Hi @Axel,

Thank you for choosing Photon!

You have two options:

  • use the index of the PhotonNetwork.PlayerList array + 1: order may not be guaranteed.
  • use PlayerNumbering utility script to have a PlayerNumber per player other than ActorNumber: add "Assets\Photon\PhotonUnityNetworking\UtilityScripts\PhotonPlayer\PlayerNumbering.cs" to the scene and then after joining get player number using player.GetPlayerNumber().

Axel
2019-09-24 13:20:55

Hi @JohnTube ,

Thank you for your answer.
I saw in another topic that the actor number is reinitialised when a player is left (https://forum.unity.com/threads/photon-unity-joining-a-room-with-game-in-progress.564043/). But for my part, it is not the case. So the player Actor number is not free when a player leaves the room? Not a solution to free this space?

I'm trying to use the GetPlayerNumber property but I need to do a SetPlayerNumber before no?

Best regards,
Axel

JohnTube
2019-09-24 13:24:29

Hi @Axel,

I saw in another topic that the actor number is reinitialised when a player is left (https://forum.unity.com/threads/photon-unity-joining-a-room-with-game-in-progress.564043/). But for my part, it is not the case. So the player Actor number is not free when a player leaves the room? Not a solution to free this space?
I think there is a misunderstanding. An actor number is never reset, never freed, never recycled, never reclaimed and can overflow and go beyond MaxPlayers. You are probably mistaking it for the PlayerNumber.
I'm trying to use the GetPlayerNumber property but I need to do a SetPlayerNumber before no?
No there is no need to explicitly set the player number as it is done for you.

Axel
2019-09-24 13:38:45

Hi @JohnTube ,

Thank you again for helping me.

Have you an example of how using the player Number?
For my part, the number of my player is -1 but I never call OnPlayerNumberingChanged.

Moreover, if you have an example of how to use it when a player leave for recovering his player number and use it if a new player arrives into the room.

Best regards,
Axel

JohnTube
2019-09-24 14:02:26

Hi @Axel,

In case you missed it, I explained the differences between Actor Number vs. Player Number here.

In PUN2, Asteroids and Cockpit demos use the PlayerNumbering utility script.

For my part, the number of my player is -1
How and when do you get this? after joining the room?
I never call OnPlayerNumberingChanged.
You don't need to explicitly call OnPlayerNumberingChanged. It's an event you subscribe to when a player number is set or changed so you are informed for example to refresh UI.

Axel
2019-09-24 15:05:39

Hi @JohnTube,

Thank you again for your answer,

I see the Asteroid demo and the use of the GetPlayerNumber property. But my case is not present into this demo and I still recovering the player actor and not the player number.
When a player is initialised of left, I send an RPC message to the other players.

In my case, I have [1,2,3,4] in the room. When the player 2 Leaves, if another player enters into the room I obtain [1,3,4,5] (even with the Get player number) but I want [1,2,3,4]. So I have to change the player number no?

Best regards,
Axel

JohnTube
2019-09-24 15:21:36

Hi @Axel,

What is the MaxPlayers value you use when you create rooms?
if it's 0 then try changing it to the exact expected max players and see if it helps.

Axel
2019-09-24 15:31:59

Hi @JohnTube

In fact, I indicate the maximum number of players when I create the room and it's work correctly because if I indicate 2, only 1 other player can join the room.

For the test is a code like that:

    private void Start()  
    {  
        PlayerNumbering.OnPlayerNumberingChanged += OnPlayerNumberingChanged;  
    }

    private void OnPlayerNumberingChanged()  
    {  
        foreach (Player p in PhotonNetwork.PlayerList)  
        {  
            Debug.Log("Player " + p.NickName +  " with the number " + p.GetPlayerNumber());  
        }  
    }  

In this case, I can have in the console "Player Player2 with the number 5"

Best regards,
Axel

Axel
2019-09-24 15:47:10

Moreover, I need to have the player index to initialise the player at the good spawn.
With the following command, I instantiate the player at the good spawn.
PhotonNetwork.Instantiate(prefabOperator.name, SpawnPoint[playerInfo.SpawnIndex()].position, SpawnPoint[playerInfo.SpawnIndex()].rotation);
It's work correctly untill a player leave the room because one spawn player is missing.
hence my problem is to re use the player index of one player who is left.

Thanks by advance,
Axel

Axel
2019-09-24 15:58:05

@JohnTube

Sorry in fact the function OnPlayerNumberingChanged() is never used. I confused the value with another Debug...
If I pass this function into a public function and I call it I have in the console "Player Player2 with the number -1"

Best regards
Axel

JohnTube
2019-09-24 16:00:53

Hi @Axel,

Did you add PlayerNumbering component to the scene (attach it to a GameObject)?
Do not call OnPlayerNumberingChanged method.
It's a callback method triggered by an event.

Axel
2019-09-24 17:19:19

Thank you very mush @JohnTube !!!

Finally, it works correctly :)
Thank you so much!

Best regards,
Axel

Axel
2019-09-25 09:16:23

Hi @JohnTube

It is possible to have a PlayerList without beeing inside the room?
Let me explain, I want to check the name of the player inside of the room and If the name already exists, the user cannot join the room with the same username. So I have to check it before to enter the room.
My problem is I never use the Room property because I use the RoomInfo property.

    public RoomInfo RoomInfo { get; private set; }

    ///   
    /// Join a room as a user.  
    ///   
    public void JoinRoom()  
    {  
        /// Check if the player name already exist  
        PhotonNetwork.NickName = RoomJoinController.join.userNameInput.text;  
        PlayerPrefs.SetString("username", RoomJoinController.join.userNameInput.text);  
        PhotonNetwork.JoinRoom(roomNameText.text);  
    }

    ///   
    /// Allow to synchronise the room information for the join panel.  
    ///   
    ///  Room inforoamtion   
    public void SetRoomInfo(RoomInfo roomInfo)  
    {  
        RoomInfo = roomInfo;  
        roomNameText.text = roomInfo.Name;  
        usersText.text = roomInfo.PlayerCount + "/" + RoomInfo.MaxPlayers;  
    }

Do you have any idea about how I can do? Because in other cases I will have a problem with my player index if 2 people have the same player name.

Best regards,
Axel

JohnTube
2019-09-25 09:26:34

Hi @Axel,

Players list is accessible only when joined to the room.
PUN, by default now, does not allow two users with the same UserId inside the same room.

Axel
2019-09-25 09:41:40

Hi @JohnTube

So it is impossible to check the player nickname inside a specific room?
In my case, I can create 2 players with the same PhotonNetwork.nickName (I don't know why).

Best regards,
Axel

JohnTube
2019-09-25 09:47:31

Hi @Axel,

Nickname and UserId are different things.
I assume you use PUN Classic, which version?
Do you use custom authentication?
Maybe you can explicitly set the UserId to the Nickname.

Axel
2019-09-25 12:51:27

Hi @JohnTube

I use PUN2 with the unity package.
Yes, when player entering or joining a room they enter the username (PhotonNetwork.nickname). When the player arrive into the room, the other player can see the username of each player.
It is not possible to use the RoomInfo to create the Room? I see in the Room property that it is possible to have the list of players inside this room. So is not possible to recover the room.players information before to join the room ?

Best regards,
Axel

JohnTube
2019-09-25 12:55:40

So is not possible to recover the room.players information before to join the room ?
Yes this is not possible.

Axel
2019-09-25 13:14:43

@JohnTube wrote:

So is not possible to recover the room.players information before to join the room ?
Yes this is not possible.

It is to bad, it is possible to count the number of player present in all the room but it is not possible to access to these players.

Thank you for your answer.
Axel

Back to top