Room CRUD

Creating Rooms

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.
  1. 1.
    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.
  2. 2.
    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.
1
public void CreateNewRoom()
2
{
3
GUI.ShowNewGamePopup((bool ok, string gameName) =>
4
{
5
if (ok)
6
{
7
roomData = new RoomCustomData();
8
roomData.name = gameName;
9
roomData.team1 = new TeamCustomData();
10
roomData.team2 = new TeamCustomData();
11
roomData.team1.players.Add(NetworkClient.Lobby.PlayerId);
12
13
// use the serializable roomData object as room's custom data.
14
NetworkClient.Lobby.CreateRoom(roomData, true, 4, (successful, reply, error) =>
15
{
16
if (successful)
17
{
18
Debug.Log("Room created " + reply);
19
// refresh the room list
20
GetRooms();
21
22
// refresh the player list
23
GetPlayersInCurrentRoom();
24
}
25
else
26
{
27
Debug.Log("Failed to create room " + error);
28
}
29
});
30
}
31
});
32
}
Copied!

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.
  1. 1.
    You can use SWLobby's member method GetRooms(int, int, action<bool, SWGetRoomReply, SWLobbyError>) to fetch rooms from the lobby server.
  2. 2.
    The room custom data is a serializable object of type RoomCustomData, you can use SWRoom's member method GetCustomData<T>() to deserialize the room custom data.
1
public void GetRooms()
2
{
3
// Get the rooms for the current page.
4
NetworkClient.Lobby.GetRooms(currentRoomPageIndex, 6, (successful, reply, error) =>
5
{
6
if (successful)
7
{
8
Debug.Log("Got rooms " + reply);
9
10
// Remove rooms in the rooms list
11
GUI.ClearRoomList();
12
13
foreach (SWRoom room in reply.rooms)
14
{
15
Debug.Log(room);
16
// Deserialize the room custom data.
17
RoomCustomData rData = room.GetCustomData<RoomCustomData>();
18
if (rData != null)
19
{
20
// Add rooms to the rooms list.
21
GUI.AddRowForRoom(rData.name, room.id, OnRoomSelected);
22
}
23
}
24
}
25
else
26
{
27
Debug.Log("Failed to get rooms " + error);
28
}
29
});
30
}
Copied!

Getting Players in a Room

You can use SWLobby's member method GetPlayersInRoom(Action<bool, SWGetPlayersReply, SWLobbyError>) to fetch the players in a room.
Add the GetPlayersInCurrentRoom() method.
1
public void GetPlayersInCurrentRoom()
2
{
3
NetworkClient.Lobby.GetPlayersInRoom((successful, reply, error) =>
4
{
5
if (successful)
6
{
7
Debug.Log("Got players " + reply);
8
9
// store the playerIds and player names in a dictionary.
10
// The dictionary is later used to populate the player list.
11
playersDict = new Dictionary<string, string>();
12
foreach (SWPlayer player in reply.players)
13
{
14
playersDict[player.id] = player.GetCustomDataString();
15
}
16
17
// fetch the room custom data.
18
GetRoomCustomData();
19
}
20
else
21
{
22
Debug.Log("Failed to get players " + error);
23
}
24
});
25
}
Copied!

Joining Rooms

You can use SWLobby's member method JoinRoom(string, Action<bool, SWJoinRoomReply, SWLobbyError>) to join a room on the lobby server.
Add the following code to the GetRooms() method.
1
public void OnRoomSelected(string roomId)
2
{
3
Debug.Log("OnRoomSelected: " + roomId);
4
// Join the selected room
5
NetworkClient.Lobby.JoinRoom(roomId, (successful, reply, error) =>
6
{
7
if (successful)
8
{
9
Debug.Log("Joined room " + reply);
10
// refresh the player list
11
GetPlayersInCurrentRoom();
12
}
13
else
14
{
15
Debug.Log("Failed to Join room " + error);
16
}
17
});
18
}
Copied!

Leaving Rooms

You can use SWLobby's member method LeaveRoom(Action<bool, SWLobbyError>) to leave a room on the lobby server.
Add the following code to the LeavingRoom() method.
1
public void LeaveRoom()
2
{
3
NetworkClient.Lobby.LeaveRoom((successful, error) =>
4
{
5
if (successful)
6
{
7
Debug.Log("Left room");
8
GUI.ClearPlayerList();
9
GetRooms();
10
}
11
else
12
{
13
Debug.Log("Failed to leave room " + error);
14
}
15
});
16
}
Copied!

Deleting Rooms

A room is automatically destroyed if there is no player in it. You don't need to manually delete rooms.