LogoLogo
  • SWNetwork SDK Overview
  • Installation
    • Custom Unity Package
  • Tutorials
    • Third-Person Shooter
      • Starter Project Introduction
      • Install SWNetwork
      • Setting up the NetworkClient
      • Sync Player Transform
      • Setting up the Scene Spawner
      • Test and Play
      • Remote Events
      • SyncProperties
      • Player Respawn
      • Updating Room States
      • Winning the Game
    • Basic Lobby
      • Lobby-start
      • Installing SWNetwork SDK and configuring NetworkClient
      • Registering Player
      • Room CRUD
      • Managing Teams
      • Chat
  • SDK
    • Network Client
      • Check-in
      • Game Servers
      • Network Status Event (beta)
      • Classes
        • NetworkClient Class
      • Methods
        • CheckIn() Method
        • ConnectToRoom(Action<bool>) Method
        • DisconnectFromRoom () Method
        • FindSpawner(ushort) Method
    • Game Play
      • Network ID
      • Realtime Agent
      • Remote Event
      • Sync Property
        • Restore Sync Properties
        • Conflict Resolution
      • SceneSpawner
      • RoomPropertyAgent
      • RoomRemoteEventAgent
    • Lobby
      • Register Player
      • Message Player
      • Create Room
      • Change Room Settings
      • Get Rooms
      • Filter Rooms
      • Join Room
      • Message Room
      • Get Players in Room
      • Get Room Custom Data
      • Change Room Custom Data
      • Kick Players
      • Start Room
      • Leave Room
      • Lobby Room Events
        • OnLobbyConnectedEvent
        • OnPlayerMessageEvent
        • OnRoomCustomDataChangeEvent
        • OnNewPlayerJoinRoomEvent
        • OnPlayerLeaveRoomEvent
        • OnNewRoomOwnerEvent
        • OnRoomStartingEvent
        • OnRoomReadyEvent
        • OnFailedToStartRoomEvent
        • OnKickedEvent
        • OnRoomMessageEvent
      • Classes
        • SWLobby Class
        • SWPlayer Class
        • SWRoom Class
        • SWRegisterReply Class
        • SWGetRoomReply Class
        • SWJoinRoomReply Class
        • SWGetRoomCustomDataReply Class
        • SWGetPlayersReply Class
        • SWLobbyIndexData Class
        • SWLobbyFilterData Class
        • SWGetRoomFilterReply Class
        • SWLobbyError Class
        • SWMessagePlayerEventData Class
        • SWMessageRoomEventData Class
        • SWRoomCustomDataChangeEventData Class
        • SWJoinRoomEventData Class
        • SWLeaveRoomEventData Class
        • SWRoomChangeOwnerEventData Class
        • SWStartRoomEventData Class
        • SWRoomReadyEventData Class
        • SWFailedToStartRoomEventData Class
      • Methods
        • Register(Action<bool, SWRegisterReply, SWLobbyError>) Method
        • MessagePlayer(string, string, Action<bool, SWLobbyError>) Method
        • CreateRoom(bool, int, Action<bool, string, SWLobbyError>) Method
        • ChangeRoomSettings(int, int, Action<bool, SWLobbyError>) Method
        • GetRooms(int, int, Action<bool, SWGetRoomReply, SWLobbyError>) Method
        • FilterRoom(SWLobbyFilterData, byte, Action<bool, SWGetRoomFilterReply, SWLobbyError>) Method
        • JoinRoom(string, Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • JoinRoomRandomly(Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • JoinOrCreateRoom(bool, int, int, Action<bool, SWJoinRoomReply, SWLobbyError>) Method
        • MessageRoom(string, Action<bool, SWLobbyError>) Method
        • GetRoomCustomData(Action<bool, SWGetRoomCustomDataReply, SWLobbyError>) Method
        • GetPlayersInRoom(Action<bool, SWGetPlayersReply, SWLobbyError>) Method
        • ChangeRoomCustomData(string, Action<bool, SWLobbyError>) Method
        • StartRoom(Action<bool, SWLobbyError>) Method
        • LeaveRoom(Action<bool, SWLobbyError>) Method
  • Open Source Software Used
    • Credits
Powered by GitBook
On this page
  • Creating Rooms
  • Getting Rooms
  • Getting Players in a Room
  • Joining Rooms
  • Leaving Rooms
  • Deleting Rooms

Was this helpful?

  1. Tutorials
  2. Basic Lobby

Room CRUD

PreviousRegistering PlayerNextManaging Teams

Last updated 5 years ago

Was this helpful?

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. 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. 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.

  3. 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.

SWLobby
GetRooms(int, int, action<bool, SWGetRoomReply, SWLobbyError>)
SWRoom
GetCustomData<T>()
GetPlayersInRoom(Action<bool, SWGetPlayersReply, SWLobbyError>)
JoinRoom(string, Action<bool, SWJoinRoomReply, SWLobbyError>)
LeaveRoom(Action<bool, SWLobbyError>)
SWLobby
CreateRoom(object, bool, int, Action<bool, SWRegisterReply, SWLobbyError>)