Search code examples
c#unity-game-enginemultiplayerphotonphoton-pun

How can I add the player to the room that has the most person but not full using Photon?


How can I add the player to the room that has the most person but not full using Photon, I'm working on a multiplayer game that using photon, I read the Photon's page documents but not seems to have any well detailed Infos, What I want is when the player click play, it took him to a server that has most people in it but not full, so it can make gameplay faster and they don't have to wait for other players, any tutorials about this?


Solution

  • use the OnRoomListUpdate function to keep track of the list of rooms

    List<RoomInfo> RoomsInfo { get; set; }
    
    void OnRoomListUpdate(List<RoomInfo> roomList)
    {
        RoomsInfo = roomList.ToList();
    }
    

    Now go throug the list to check that the player count is not full:

    // Select non full rooms
    var validRooms = RoomsInfo.Where(x => x.PlayerCount != x.MaxPlayers);
    
    // Select the one of the most players
    var room = validRooms.OrderByDescending(x => x.PlayerCount).FirstOrDefault();
    

    And that would be the room you want.