I'm trying to make a multiplayer game where there are multiple levels. All these levels will have different rooms, each with a max player count of 4.
Basically,
- player selects a level
- check if there is an existing room for that level that hasn't met the max player count
- if there is such room, join the room; if not, create a new room for the level
- wait until the room's player count reaches 4
- start game
I know that Photon has something called JoinOrCreateRoom
with max player options, but with this, wouldn't there be no way to create rooms with different names?
My idea is that the room names would be: levelNumber + "_" + orderOfTheRoomInLevel
For example, the 3rd room of level 1 would be named 1_3. This way, I could keep track of how many rooms there are per level by increasing the number whenever the player creates a room.
My code is:
public void StartRoomMatch(int level)
{
roomList = PhotonNetwork.GetRoomList().ToList();
RoomOptions options = new RoomOptions();
options.MaxPlayers = 4;
options.EmptyRoomTtl = 1;
if(CheckIfRoomExists(level + "_" + roomNumsPerLevel[level]))
{
if(player count of room by name < 4)
{
PhotonNetwork.JoinRoom(level + "_" + roomNumsPerLevel[level]);
}
else
{
PhotonNetwork.CreateRoom((level + "_" + (++roomNumsPerLevel[level]), options, TypedLobby.Default);
}
}
else //this is the first room being created in the level
{
PhotonNetwork.CreateRoom((level + "_" + roomNumsPerLevel[level], options, TypedLobby.Default);
roomNumsPerLevel[level]++;
}
// PhotonNetwork.JoinOrCreateRoom(level + "", options, TypedLobby.Default);
}
private bool CheckIfRoomExists(string roomName)
{
RoomInfo room = roomList.FirstOrDefault(r => r.Name == roomName);
return (room != null);
}
The problem is, I don't know what to fill in in the second if statement.
Is this an effective way of level matchmaking? If not, what would be a good way? (It is my first time using Photon Server so I would appreciate a detailed explanation) If yes, how do I get the player count of a room by the room's name?
orderOfTheRoomInLevel
part and only check against the level \$\endgroup\$