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

Was this helpful?

  1. Tutorials
  2. Basic Lobby

Managing Teams

Managing teams of a room is a little complicated. You need to subscribe to a few lobby events and modify the room custom data. Here is a quick rundown of the workflow:

  • Subscribe to the lobby events.

  • Room owner should assign new players who joined the room to a team.

  • Room owner should remove players who left the room from all teams.

  • All room members should refresh the players list when room custom data is changed to reflect the updated teams.

Only the room owner can change the room custom data.

Lobby events

Invoked when new players join the local player's room.

Invoked when players leave the local player's room.

Invoked when the room custom data is updated.

Open the LobbyDemo.cs script and add the following code to Start() and onDestroy() to subscribe to the lobby events.

void Start()
{
	// Subscribe to Lobby events
	NetworkClient.Lobby.OnNewPlayerJoinRoomEvent += Lobby_OnNewPlayerJoinRoomEvent;
	NetworkClient.Lobby.OnPlayerLeaveRoomEvent += Lobby_OnPlayerLeaveRoomEvent;
	NetworkClient.Lobby.OnRoomCustomDataChangeEvent += Lobby_OnRoomCustomDataChangeEvent;
}

void onDestroy()
{
	// Unsubscrible to Lobby events
	NetworkClient.Lobby.OnNewPlayerJoinRoomEvent -= Lobby_OnNewPlayerJoinRoomEvent;
	NetworkClient.Lobby.OnPlayerLeaveRoomEvent -= Lobby_OnPlayerLeaveRoomEvent;
	NetworkClient.Lobby.OnRoomCustomDataChangeEvent -= Lobby_OnRoomCustomDataChangeEvent;
}

Next, Implement the OnNewPlayerJoinRoomEvent handler method.

void Lobby_OnNewPlayerJoinRoomEvent(SWJoinRoomEventData eventData)
{
	Debug.Log("Player joined room");
	Debug.Log(eventData);

	// Store the new playerId and player name pair
	playersDict[eventData.newPlayerId] = eventData.GetString();

	if (NetworkClient.Lobby.IsOwner)
	{   
		// Find the smaller team and assign the new player to it.
		if(roomData.team1.players.Count < roomData.team2.players.Count)
		{
			roomData.team1.players.Add(eventData.newPlayerId);
		}
		else
		{
			roomData.team2.players.Add(eventData.newPlayerId);
		}

		// Update the room custom data
		NetworkClient.Lobby.ChangeRoomCustomData(roomData, (bool successful, SWLobbyError error) =>
		{
			if (successful)
			{
				Debug.Log("ChangeRoomCustomData successful");
				RefreshPlayerList();
			}
			else
			{
				Debug.Log("ChangeRoomCustomData failed: " + error);
			}
		});
	}
}

Implement the OnPlayerLeaveRoomEvent handler method.

void Lobby_OnPlayerLeaveRoomEvent(SWLeaveRoomEventData eventData)
{
	Debug.Log("Player left room: " + eventData);

	if (NetworkClient.Lobby.IsOwner)
	{
		// Remove the players from both team.
		roomData.team2.players.RemoveAll(eventData.leavePlayerIds.Contains);
		roomData.team1.players.RemoveAll(eventData.leavePlayerIds.Contains);

		// Update the room custom data
		NetworkClient.Lobby.ChangeRoomCustomData(roomData, (bool successful, SWLobbyError error) =>
		{
			if (successful)
			{
				Debug.Log("ChangeRoomCustomData successful");
				RefreshPlayerList();
			}
			else
			{
				Debug.Log("ChangeRoomCustomData failed: " + error);
			}
		});
	}
}

Implement the OnRoomCustomDataChangeEvent handler method.

void Lobby_OnRoomCustomDataChangeEvent(SWRoomCustomDataChangeEventData eventData)
{
	Debug.Log("Room custom data changed: " + eventData);

	SWRoom room = NetworkClient.Lobby.RoomData;
	roomData = room.GetCustomData<RoomCustomData>();

	// Room custom data changed, refresh the player list.
	RefreshPlayerList();
}
PreviousRoom CRUDNextChat

Last updated 5 years ago

Was this helpful?

OnNewPlayerJoinRoomEvent
OnPlayerLeaveRoomEvent
OnRoomCustomDataChangeEvent