Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Lobby Players does not update on player disconnect

Discussion in 'Lobby' started by Bram280, Jul 21, 2023.

  1. Bram280

    Bram280

    Joined:
    Dec 1, 2019
    Posts:
    5
    Every two seconds I call:
    Code (CSharp):
    1. joinedLobby = await LobbyService.Instance.GetLobbyAsync(joinedLobby.Id);
    2. 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?
     
  2. bartj-unity

    bartj-unity

    Unity Technologies

    Joined:
    Sep 9, 2021
    Posts:
    25
    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.
     
  3. Bram280

    Bram280

    Joined:
    Dec 1, 2019
    Posts:
    5
    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?
     
  4. bartj-unity

    bartj-unity

    Unity Technologies

    Joined:
    Sep 9, 2021
    Posts:
    25
    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.
     
  5. Bram280

    Bram280

    Joined:
    Dec 1, 2019
    Posts:
    5
    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:
    Code (CSharp):
    1.         var callbacks = new LobbyEventCallbacks();
    2.         callbacks.PlayerLeft += OnPlayerLeft;
    3.         callbacks.LobbyChanged += OnLobbyChanged;
    4.         callbacks.PlayerJoined += OnPlayerJoined;
    5.         try
    6.         {
    7.             await Lobbies.Instance.SubscribeToLobbyEventsAsync(joinedLobby.Id, callbacks);
    8.         }
    9.         catch (LobbyServiceException ex)
    10.         {
    11.             switch (ex.Reason)
    12.             {
    13.                 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;
    14.                 case LobbyExceptionReason.SubscriptionToLobbyLostWhileBusy: Debug.LogError($"Subscription to lobby events was lost while it was busy trying to subscribe. Exception Message: {ex.Message}"); throw;
    15.                 case LobbyExceptionReason.LobbyEventServiceConnectionError: Debug.LogError($"Failed to connect to lobby events. Exception Message: {ex.Message}"); throw;
    16.                 default: throw;
    17.             }
    18.         }
    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: https://docs.unity.com/lobby/en-us/manual/lobby-events