Lobby Players not updating correctly

I am using Lobby 1.1.0 with the callbacks for Lobby update.
This is the code I am using, I put a print statement in OnLobbyChanged:

private static async Task SubscribeToLobbyChanges()
    {
        LobbyEventCallbacks callbacks = new LobbyEventCallbacks();
        callbacks.LobbyChanged += OnLobbyChanged;
        callbacks.KickedFromLobby += OnKickedFromLobby;
        callbacks.LobbyEventConnectionStateChanged += OnLobbyEventConnectionStateChanged;
        try
        {
            _lobbyEvents = await Lobbies.Instance.SubscribeToLobbyEventsAsync(JoinedLobby.Id, callbacks);
        }
        catch (LobbyServiceException ex)
        {
            switch (ex.Reason)
            {
                case LobbyExceptionReason.AlreadySubscribedToLobby: Debug.LogWarning($"Already subscribed to lobby[{JoinedLobby.Id}]. We did not need to try and subscribe again. Exception Message: {ex.Message}"); break;
                case LobbyExceptionReason.SubscriptionToLobbyLostWhileBusy: Debug.LogError($"Subscription to lobby events was lost while it was busy trying to subscribe. Exception Message: {ex.Message}"); throw;
                case LobbyExceptionReason.LobbyEventServiceConnectionError: Debug.LogError($"Failed to connect to lobby events. Exception Message: {ex.Message}"); throw;
                default: throw;
            }
        }
    }
private static void OnLobbyChanged(ILobbyChanges changes)
    {
        if (changes.LobbyDeleted)
        {
            OnLobbyDisconnect?.Invoke();
            OnLobbyDisconnect = null;
            RefreshLobbyList();
        }
        else
        {
            changes.ApplyToLobby(JoinedLobby);
            print(JoinedLobby.Players.Count);
            OnLobbyRefresh?.Invoke();
        }
    }

My flow is this:
Player 1 is creating a lobby. “1” get printed and I can see them in the lobby screen.
Player 2 connects to the lobby. “2” gets printed in Player 1’ editor and I can see both players in the lobby screen.
Player 2 leaves, “1” gets printed in Player 1’s editor and only Player 1 is on the lobby screen.
Player 2 rejoins the same lobby. “1” gets printed in Player 1’s editor. I can see both players on Player 2’s editor, but only Player 1 on Player 1’s editor.
I also tested and only changes.Version.Changed is true, all the other changes return false.
I posted a video below to make this clearer.
73sru7
I fixed the problem by making a request for the lobby in OnLobbyChanged, but that obviously isn’t the intended way to do this.

private async static void OnLobbyChanged(ILobbyChanges changes)
    {
        if (changes.LobbyDeleted)
        {
            OnLobbyDisconnect?.Invoke();
            OnLobbyDisconnect = null;
            RefreshLobbyList();
        }
        else
        {
            changes.ApplyToLobby(JoinedLobby);
            JoinedLobby = await LobbyService.Instance.GetLobbyAsync(JoinedLobby.Id);
            OnLobbyRefresh?.Invoke();
        }
    }
1 Like

I was facing a similar issue - I would receive the LobbyChanged callback, but the object would be ‘empty’ - everything false or zeroed out, including the LastUpdated field. Only Version would have a legitimate value. So callbacks like DataAdded were never fired.

What I found is that Unity’s lobby code maintains an internal cache of lobby data to compare changes against (called JoinedLobbyCache). Methods that populate this cache include (as of 1.1.0-pre.4):

  • CreateLobbyAsync
  • CreateOrJoinLobbyAsync
  • GetLobbyAsync
  • JoinLobbyByCodeAsync
  • QuickJoinLobbyAsync
  • ReconnectToLobbyAsync

I had only called QueryLobbiesAsync and then JoinLobbyByIdAsync, neither of which put my lobby into the cache, so when I received my first set of changes for LobbyChanged, it couldn’t compute an actual diff, and gave me an empty changes object with my LobbyChanged callback. At least, I think that’s what was going on.

My solution was to simply call GetLobbyAsync right before calling JoinLobbyByIdAsync. Then changes started flowing from my subscription - DataAdded etc.

Looks like my issue is fixed in pre.5! Thanks folks!

1 Like