How To Get Players In Room

Options
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
    Options
    How about PhotonNetwork.CurrentRoom.Players?
  • GalacticFalcon
    Options
    @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
    edited May 2020
    Options
    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
    edited May 2020
    Options
    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
    edited May 2020
    Options
    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 <SpawnPoint>
    // 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
    Options
    @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
    Lethn
    edited May 2020
    Options
    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
    edited May 2020
    Options
    @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
    Options
    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.