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 How do I remove a player from a lobby when they close the window?

Discussion in 'Lobby' started by kadenjantz, Jun 26, 2023.

  1. kadenjantz

    kadenjantz

    Joined:
    Jan 30, 2021
    Posts:
    1
    Hello, when a client closes their window, they are not leaving the lobby. This is a particularly big problem if they are the host, because then the host never transfers to anyone else.

    I tried to make the client leave like this:
    Code (CSharp):
    1.     void OnApplicationQuit() {
    2.         Debug.Log("QUIT");
    3.  
    4.         Leave();
    5.     }
    6.  
    7.     async void Leave() {
    8.         await LobbyService.Instance.RemovePlayerAsync(LobbyManager.Instance.joinedLobby.Id, AuthenticationService.Instance.PlayerId);
    9.     }
    The log did appear, but the client did not leave the lobby. This also didn't work:
    Code (CSharp):
    1.     void OnApplicationQuit() {
    2.         Debug.Log("QUIT");
    3.  
    4.         LobbyService.Instance.RemovePlayerAsync(LobbyManager.Instance.joinedLobby.Id, AuthenticationService.Instance.PlayerId);
    5.     }
    I'm not sure what else to try. Any ideas would be much appreciated, thanks!
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    156
    Hi @kadenjantz ,
    You have to set "Disconnect Removal Time" variable to 10 second. 10 is minimum value.
    This properties is in the Dashboard=>Lobby =>Config page.
     
  3. earthcrosser

    earthcrosser

    Joined:
    Oct 13, 2010
    Posts:
    121
    Looks like this is a pretty old thread but I also ran into this problem when testing project. I configured the lobby as suggested above with no effect.
    I came up with this solution after looking at the example lobby project that Unity provides.

    Objective: Ensure that when the application is attempting to quit, any player that has joined a lobby is properly removed before the application fully exits.

    Implementation:

    1. Event Subscription:
      • During initialization (UnityGameServicesInitializer_OnInitialized method), we subscribe to the Application.wantsToQuit event. This event is triggered when the application is preparing to exit.
    2. Checking Lobby Status:
      • In the Application_wantsToQuit event handler, we evaluate if the player is currently in a lobby using the HasJoinedLobby() method.
      • If the player has not joined a lobby, the method will immediately allow the application to exit by returning true for canQuit.
      • If the player is in a lobby, we initiate the process to remove the player from the lobby by invoking the LeaveBeforeQuit coroutine.
    3. Player Removal Process:
      • The LeaveBeforeQuit coroutine initiates the RemovePlayerAsync method, which is responsible for the asynchronous task of removing the player from the lobby.
      • We then pause the coroutine execution and wait until the removal task is completed using the WaitUntil method.
      • Once the player is successfully removed, the Application.Quit() method is called again to exit the application.
    4. Asynchronous Player Removal:
      • The RemovePlayerAsync method uses Unity's LobbyService to perform the asynchronous operation of removing the player from the specified lobby.
      • If the removal is successful, we reset the joinedLobby to null.
      • In case of any exceptions or errors during this process, an error message is logged.
    Outcome: Through this approach, we ensure that any player in a lobby is gracefully removed before the application terminates. If the removal is successful, the application is allowed to quit, ensuring a clean exit.


    Code (CSharp):
    1.  
    2. protected override void UnityGameServicesInitializer_OnInitialized()
    3.         {
    4.             base.UnityGameServicesInitializer_OnInitialized();
    5.             Application.wantsToQuit += Application_wantsToQuit;
    6.             StartCoroutine(HeartbeatCouroutine());
    7.         }
    8. private bool Application_wantsToQuit()
    9.         {
    10.             bool canQuit = !HasJoinedLobby();
    11.             if (HasJoinedLobby())
    12.             {
    13.                 StartCoroutine(LeaveBeforeQuit(joinedLobby.Id, AuthServiceHandler.UnityAuthorizationServicePlayerId));
    14.             }
    15.             return canQuit;
    16.         }
    17. IEnumerator LeaveBeforeQuit(string lobbyId, string playerId)
    18.         {
    19.             var task = RemovePlayerAsync(lobbyId, playerId);
    20.             yield return new WaitUntil(() => task.IsCompleted);
    21.             Application.Quit();
    22.         }
    23. private async Task RemovePlayerAsync(string lobbyId, string playerId)
    24.         {
    25.             if (!IsServiceReady) return;
    26.             try
    27.             {
    28.                 await LobbyService.Instance.RemovePlayerAsync(lobbyId, playerId);
    29.                 joinedLobby = null;
    30.             }
    31.             catch
    32.             {
    33.                 Debug.LogError("Failed to remove player");
    34.             }
    35.         }
     
    Last edited: Aug 18, 2023