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.

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);
}

Last updated