In this tutorial, we allow players to pick room names. The starter project already implemented the popup view to let players enter room names. You can use LobbyGUI's member method ShowNewGamePopup(Action<bool, string>) to display the popup view.
Open the LobbyDemo.cs script and add the following code to the CreateNewRoom() method.
If the player entered a name and clicked the OK button in the popup, the ok bool value will be true and gameName string value will be the value that the player entered.
Create a RoomCustomData object, initialize its team variables. Also, you can assign the local player to team1 by adding local player's PlayerId to team1's players' list.
You can use 's member method to create the new room.
public void CreateNewRoom()
{
GUI.ShowNewGamePopup((bool ok, string gameName) =>
{
if (ok)
{
roomData = new RoomCustomData();
roomData.name = gameName;
roomData.team1 = new TeamCustomData();
roomData.team2 = new TeamCustomData();
roomData.team1.players.Add(NetworkClient.Lobby.PlayerId);
// use the serializable roomData object as room's custom data.
NetworkClient.Lobby.CreateRoom(roomData, true, 4, (successful, reply, error) =>
{
if (successful)
{
Debug.Log("Room created " + reply);
// refresh the room list
GetRooms();
// refresh the player list
GetPlayersInCurrentRoom();
}
else
{
Debug.Log("Failed to create room " + error);
}
});
}
});
}
Getting Rooms
You created a new room on the lobby server. You need to fetch the room from the lobby server.
Open the LobbyDemo.cs script and add the following code to the GetRooms() method.
public void GetRooms()
{
// Get the rooms for the current page.
NetworkClient.Lobby.GetRooms(currentRoomPageIndex, 6, (successful, reply, error) =>
{
if (successful)
{
Debug.Log("Got rooms " + reply);
// Remove rooms in the rooms list
GUI.ClearRoomList();
foreach (SWRoom room in reply.rooms)
{
Debug.Log(room);
// Deserialize the room custom data.
RoomCustomData rData = room.GetCustomData<RoomCustomData>();
if (rData != null)
{
// Add rooms to the rooms list.
GUI.AddRowForRoom(rData.name, room.id, OnRoomSelected);
}
}
}
else
{
Debug.Log("Failed to get rooms " + error);
}
});
}
Getting Players in a Room
Add the GetPlayersInCurrentRoom() method.
public void GetPlayersInCurrentRoom()
{
NetworkClient.Lobby.GetPlayersInRoom((successful, reply, error) =>
{
if (successful)
{
Debug.Log("Got players " + reply);
// store the playerIds and player names in a dictionary.
// The dictionary is later used to populate the player list.
playersDict = new Dictionary<string, string>();
foreach (SWPlayer player in reply.players)
{
playersDict[player.id] = player.GetCustomDataString();
}
// fetch the room custom data.
GetRoomCustomData();
}
else
{
Debug.Log("Failed to get players " + error);
}
});
}
Joining Rooms
Add the following code to the GetRooms() method.
public void OnRoomSelected(string roomId)
{
Debug.Log("OnRoomSelected: " + roomId);
// Join the selected room
NetworkClient.Lobby.JoinRoom(roomId, (successful, reply, error) =>
{
if (successful)
{
Debug.Log("Joined room " + reply);
// refresh the player list
GetPlayersInCurrentRoom();
}
else
{
Debug.Log("Failed to Join room " + error);
}
});
}
Leaving Rooms
Add the following code to the LeavingRoom() method.
public void LeaveRoom()
{
NetworkClient.Lobby.LeaveRoom((successful, error) =>
{
if (successful)
{
Debug.Log("Left room");
GUI.ClearPlayerList();
GetRooms();
}
else
{
Debug.Log("Failed to leave room " + error);
}
});
}
Deleting Rooms
A room is automatically destroyed if there is no player in it. You don't need to manually delete rooms.
You can use 's member method to fetch rooms from the lobby server.
The room custom data is a serializable object of type RoomCustomData, you can use 's member method to deserialize the room custom data.
You can use SWLobby's member method to fetch the players in a room.
You can use SWLobby's member method to join a room on the lobby server.
You can use SWLobby's member method to leave a room on the lobby server.