Lobby - Updating player data is not triggering events, and lobby is not updated even with getLobbyAsync

Hi, I have a few problems with Lobby.

  • When I make a call to UpdatePlayerAsync, but it sometimes triggers PlayerDataAdded event, and sometimes don’t. After that I added the EnableLocalPlayerLobbyEvents(true) line which seems to solve this issue. But I’m not sure since before this setting, it was also sending these events from time to time, but not consistently.

  • Problem 2, after updating player data (for player name from auth service), I don’t know how to get the full lobby and it’s players objects with up to date information. When I poll the lobby with GetLobbyAsync, it doesn’t include any information for the players, only the player.id

What is the recommended way for this? I saw the lobby sample but it’s too advanced/complex for my use case, I don’t wanna cache and check every single key locally, what’s the best method for this? Thanks.

You should include the code in case there’s an obvious issue.

try
{
    var lobby = await LobbyService.Instance.GetLobbyAsync("lobbyId");
}
catch (LobbyServiceException e)
{
    Debug.Log(e);
}

In this code, the received lobby doesn’t have any of the player data records.

Another question is, when a new player joins, they dont have the most updated information for players joined before him, related to the same issue of just fetching players.

Also, I see again that playerDataAdded and playerDataChanged dont trigger sometimes even after that enable trick.


Edit: I solved my issue, it was my lack of understanding.

For the callbacks not being triggered, you need to assign callbacks before registering for events. Such as:

callbacks.PlayerJoined += (List<LobbyPlayerJoined> lobbyPlayerJoined) => { TryPollLobby("player joined"); };
callbacks.PlayerLeft += (List<int> leftPlayers) => { TryPollLobby("player left"); };
await LobbyService.Instance.SubscribeToLobbyEventsAsync(id, callbacks);

Instead of:

await LobbyService.Instance.SubscribeToLobbyEventsAsync(id, callbacks);
callbacks.PlayerJoined += (List<LobbyPlayerJoined> lobbyPlayerJoined) => { TryPollLobby("player joined"); };
callbacks.PlayerLeft += (List<int> leftPlayers) => { TryPollLobby("player left"); };

For the other issue, the lobby actually has the player data, but I was doing an incorrect iteration to get the data. Here is a working example.

public void UpdatePlayerList()
{
	var thisPlayerId = AuthenticationService.Instance.PlayerId;

	foreach (var player in lobby.Players)
	{
		var playerName = player.Id;
		var playerId = player.Id;
		var colorIndex = 0;
		var teamIndex = 0;
		var isReady = false;

		if (player.Data != default)
		{
			foreach (var key in player.Data.Keys)
			{
				var value = player.Data[key];
				switch (key)
				{
					case GameDataHandler.KEY_LOBBY_PLAYER_DATA_NAME:
						playerName = value.Value;
						break;
					case GameDataHandler.KEY_LOBBY_PLAYER_DATA_COLOR:
						colorIndex = Convert.ToInt32(value.Value);
						break;
					case GameDataHandler.KEY_LOBBY_PLAYER_DATA_TEAM:
						teamIndex = Convert.ToInt32(value.Value);
						break;
					case GameDataHandler.KEY_LOBBY_PLAYER_DATA_READY:
						isReady = value.Value == "1" ? true : false;
						break;
					default:
						break;
				}
			}
		}

		var isHost = lobby.HostId == playerId;
		var isOwner = thisPlayerId == playerId;
	}
}

The confusion was trying to update the lobby by getting pieces of information on callbacks. My solution was to subscribe to every event to trigger a lobby poll, but also since there is a rate limit, it only registers a function to be called in 1.1 seconds, if there is another lobby poll request, it cancels the request scheduled previously and schedules a new one. This way the most frequent call is always 1.1 seconds, which is lower than the rate limit of 1 request per second.

I don’t know how to contribute to docs but definitely the docs need more clarification.

Hopefully this helps somebody.