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. SDK
  2. Lobby

Lobby Room Events

NetworkClient.Lobby invokes the following events to help you keep track of rooms states and receive messages from other players.

Some of the events (OnLobbyConnectedEvent, OnRoomReadyEvent) are important to keep the matchmaking process running.

If you have a lobby interface, you might be interested in events like OnNewPlayerJoinRoomEvent, OnPlayerLeaveRoomEvent, and OnRoomCustomDataChangeEvent.

Event

Invoked when the local player connected to the lobby server.

Invoked when a new player message is received.

Invoked when the room custom data is updated.

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

Invoked when players leave the local player's room.

Invoked when the room owner is changed.

Invoked when the room changes to the Starting state.

Game servers are being prepared for the room.

If no available game servers are found, FailedToStartRoomEvent will be invoked.

If game servers are prepared, RoomReadyEvent will be invoked.

Invoked when the room changes to the Rooms Set state.

Game servers for the room are prepared.

Use NetworkClient.Instance.ConnectToRoom(Action) to connect to the game servers.

Invoked when the room fails to start.

No game servers are available.

Invoked when a new room message is received.

Invoked when the player is kicked from the room by the room owner.

Example

In this example, we added listeners to the lobby room events in the Start() method and removed the listeners in the OnDestroy() method.

void Start() {
	NetworkClient.Lobby.OnRoomReadyEvent += Lobby_OnRoomReadyEvent;
	NetworkClient.Lobby.OnNewPlayerJoinRoomEvent += Lobby_OnNewPlayerJoinRoomEvent;
	NetworkClient.Lobby.OnNewRoomOwnerEvent += Lobby_OnNewRoomOwnerEvent;
	NetworkClient.Lobby.OnRoomStartingEvent += Lobby_OnRoomStartingEvent;
	NetworkClient.Lobby.OnPlayerLeaveRoomEvent += Lobby_OnPlayerLeaveRoomEvent;
	NetworkClient.Lobby.onRoomCustomDataChangeEvent += Lobby_OnRoomCustomDataChangeEvent;
	NetworkClient.Lobby.onFailedToStartRoomEvent += Lobby_OnFailedToStartRoomEvent;
}

void onDestroy() {
	NetworkClient.Lobby.OnRoomReadyEvent -= Lobby_OnRoomReadyEvent;
	NetworkClient.Lobby.OnNewPlayerJoinRoomEvent -= Lobby_OnNewPlayerJoinRoomEvent;
	NetworkClient.Lobby.OnNewRoomOwnerEvent -= Lobby_OnNewRoomOwnerEvent;
	NetworkClient.Lobby.OnRoomStartingEvent -= Lobby_OnRoomStartingEvent;
	NetworkClient.Lobby.OnPlayerLeaveRoomEvent -= Lobby_OnPlayerLeaveRoomEvent;
	NetworkClient.Lobby.onRoomCustomDataChangeEvent -= Lobby_OnRoomCustomDataChangeEvent;
	NetworkClient.Lobby.onFailedToStartRoomEvent -= Lobby_OnFailedToStartRoomEvent;
}

Then, we implemented the listeners

// lobby delegate events
void Lobby_OnRoomReadyEvent(SWRoomReadyEventData eventData) {
	Debug.Log("Room is ready: roomId= " + eventData.roomId);
	NetworkClient.Instance.ConnectToRoom(ConnectedToRoom);
}

void ConnectedToRoom(bool connected) {
	if (connected) {
		Debug.Log("Connected to room");
		SceneManager.LoadScene("Game");
	}
	else {
		Debug.Log("Failed to connect to room");
	}
}

void Lobby_OnNewPlayerJoinRoomEvent(SWJoinRoomEventData eventData) {
	Debug.Log("Player joined room: roomId= " + eventData.roomId + " newPlayerId= " + eventData.newPlayerId);
	if (NetworkClient.Lobby.IsOwner) {
	
		// room owner assigns the new player to team2.
		SWRoom room = NetworkClient.Lobby.RoomData;
		RoomData roomData = room.GetCustomData < RoomData > ();
		roomData.team2.players.Add(eventData.newPlayerId);

		NetworkClient.Lobby.ChangeRoomCustomData(roomData, (bool succeed, object obj) = >{
			Debug.Log("ChangeRoomCustomData successful=" + succeed + " obj= " + obj);
		});
	}
}

void Lobby_OnNewRoomOwnerEvent(SWRoomChangeOwnerEventData eventData) {
	Debug.Log("Room owner changed: roomId= " + eventData.roomId + " newOwnerId= " + eventData.newOwnerId);
}

void Lobby_OnRoomStartingEvent(SWStartRoomEventData eventData) {
	Debug.Log("Room is starting: roomId= " + eventData.roomId + " ownerId= " + eventData.ownerId);
}

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

	if (NetworkClient.Lobby.IsOwner) {
		
		// room owner remove the player from both team.
		SWRoom room = NetworkClient.Lobby.RoomData;
		RoomData roomData = room.GetCustomData < RoomData > ();
		roomData.team2.players.Remove(eventData.leavePlayerId);
		roomData.team1.players.Remove(eventData.leavePlayerId);

		NetworkClient.Lobby.ChangeRoomCustomData(roomData, (bool succeed, object obj) = >{
			Debug.Log("ChangeRoomCustomData successful=" + succeed + " obj= " + obj);
		});
	}
}

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

	SWRoom room = NetworkClient.Lobby.RoomData;
	RoomData roomData = room.GetCustomData < RoomData > ();
	
	// update the player List
	RefreshPlayerList(roomData);
}

void Lobby_OnFailedToStartRoomEvent(SWFailedToStartRoomEventData eventData) {
	Debug.Log("Failed to start room: " + eventData);
}
PreviousLeave RoomNextOnLobbyConnectedEvent

Last updated 5 years ago

Was this helpful?

OnLobbyConnectedEvent
OnPlayerMessageEvent
OnRoomCustomDataChangeEvent
OnNewPlayerJoinRoomEvent
OnPlayerLeaveRoomEvent
OnNewRoomOwnerEvent
OnRoomStartingEvent
OnRoomReadyEvent
OnFailedToStartRoomEvent
OnRoomMessageEvent
OnKickedEvent