Lobby Players does not update on player disconnect

Every two seconds I call:

joinedLobby = await LobbyService.Instance.GetLobbyAsync(joinedLobby.Id);
Debug.Log(joinedLobby.Players.Count)

If I have two players connected in a lobby and I close on the players window, it never disconnects from the lobby it seems. (I have Disconnect Removal Time set to 15s)
The player count is always 2 according to the debug.log.

Is it possible that my joinedLobby variable or the Players does not get updated correctly from the GetLobbyAsync?

Are you integrated with Relay or using the Lobby real-time event updates? You won’t get any player disconnections at all if neither of those things are being used.

Ah so instead of using the GetLobbyAsync every two seconds I should use the Lobby events? And GetLobbyAsync does not update the lobby information itself?

GetLobbyAsync is actually doing what you expect - if I understand your setup correctly, the problem is that the players aren’t changing, so showing that the player count is always 2 is expected. The reason I mention Relay or the Lobby events is because those are the 2 ways that the Lobby service actually detects players disconnecting.

In other words, if your code only creates a lobby and joins a 2nd player into it without doing anything else, and then you close the windows, the Lobby service will not be able to detect a disconnection and remove the player. You need to either use Relay or events to get that functionality.

Thanks the lobby event system worked for me.
For future people reading this, the way I fixed my issue was downloading the latest lobby version (1.1.0-pre.5)
and adding the following callbacks and subscribing to the relevant lobby:

        var callbacks = new LobbyEventCallbacks();
        callbacks.PlayerLeft += OnPlayerLeft;
        callbacks.LobbyChanged += OnLobbyChanged;
        callbacks.PlayerJoined += OnPlayerJoined;
        try
        {
            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;
            }
        }

Where in my OnPlayerLeft()/OnPlayerJoined function I update the UI of my lobby and in my OnLobbyChanged() function I check if a new game has been started and join the according Relay.
joinedLobby here is a variable which keeps track of the lobby the player has joined.
Code like seen in the docs: Lobby events

For me onplayerleft still doesn’t work when I close the window it works when I leave normally I don’t understand

2 Likes