How To Get Players In 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.

How To Get Players In Room

GalacticFalcon
2020-05-09 15:07:03

hey guys i am having trouble as to how to get all the players in the current room so i can set every players position differently and not all of them spawning at the same position before doing

PhotonNetwork.Instantiate(playerPrefab.name, SpawnPositions[0].position, Quaternion.Euler(0, 0, 0));

Comments

hyperbting
2020-05-09 15:55:47

How about PhotonNetwork.CurrentRoom.Players?

GalacticFalcon
2020-05-09 16:52:52

@hyperbting yes i can do that but will i be able to set every single players position individually ?? sorry for dumb questions :D

Little Angel
2020-05-09 17:28:07

Not sure if this helps but looking here:
https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_pun_1_1_photon_network.html

... there is:
static Player[] PlayerList[get]
A sorted copy of the players-list of the current room.

There is a note there somewhere that says:
Use PhotonNetwork.PlayerList.Length or PhotonNetwork.CurrentRoom.PlayerCount to get the count of players in the room you're in

Does this help?

You should be able to iterate over that list and assign spawnpoints, no?

GalacticFalcon
2020-05-09 18:29:43

@Little Angel wrote: »

Not sure if this helps but looking here:
https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_pun_1_1_photon_network.html

... there is:
static Player[] PlayerList[get]
A sorted copy of the players-list of the current room.

There is a note there somewhere that says:
Use PhotonNetwork.PlayerList.Length or PhotonNetwork.CurrentRoom.PlayerCount to get the count of players in the room you're in

Does this help?

You should be able to iterate over that list and assign spawnpoints, no?

var player1 = PhotonNetwork.CurrentRoom.Players.ElementAt(0);  
var player2 = PhotonNetwork.CurrentRoom.Players.ElementAt(1);

yes i do get the players but i don't know still how to set those players positions!! @"Little Angel"

Little Angel
2020-05-09 22:18:27

Just off the top of my head, and I've not coded for a while, you'd do the following in logic (all pseudo code as I've not looked in detail).

// Create the concept of a SpawnPoint with a position and a rotation
// Create an empty list of spawn points with a List
// Set every spawn point in the list to a valid value of a position and a rotation on your map - this could be done with empty game objects in your scene, or just numerical values of your choice.

then when you run the game

// Get the list of players in the room with something like myPlayerList = GetPlayerList()
// iterate through them with for each player in my player list (using a for loop or a foreach, as you see fit) set player (i)'s spawn position and spawn rotation to the values held in spawnPoint(i) - at this step you could get fancy and pick a random spawnpoint from your list, so it's not just 1:1 from the "get player" call.

GalacticFalcon
2020-05-10 00:43:01

@"Little Angel" am i going in the right path?

        [SerializeField]  
        private GameObject playerPrefab;  
        [SerializeField]  
        public Transform[] SpawnPositions;


foreach (var player in PhotonNetwork.PlayerList) // 2 Players Room  
{

}  

if only i knew how to instantiate Gameobjects for different players instead of all the players at once by :neutral:

PhotonNetwork.Instantiate

Lethn
2020-05-10 19:01:31

Use if (photonView.isMine) to deal with individual players or objects when instantiating across a network, in fact you use this for most things in photon. Be sure to check out documentation on isMine as it's very important for this kind of thing you won't get far without it.

GalacticFalcon
2020-05-10 23:13:33

@Lethn i am trying to make a round robin spawning system for instantiating objects. is there a way of getting the Player's actor number who a PhotonView belongs to

Tobias
2020-05-12 15:20:52

PUN v2.18 has a component called OnJoinedInstantiate. It does round robin, based on the players ActorNumbers, if I recall correctly. This may help.

ActorNumbers are cool, as they never get reused. On the other hand, if a player leaves and another joins, you may have ActorNumber 5 in a 4 player room. This may break the OnJoinedInstantiate.
But there is a PlayerNumbering script, which might be useful to get "Player Numbers" done (e.g 1..4 for a 4 player room).

Hope some of that helps.

Back to top