Search Unity

Question Updating Lobby Settings

Discussion in 'Lobby' started by GlomyShadow, Nov 26, 2022.

  1. GlomyShadow

    GlomyShadow

    Joined:
    Jan 8, 2022
    Posts:
    4
    Hello, for some reason this code isn't working, what I am trying to do is to update the settings of a lobby before a game begins, but for some reason the lobby's settings don't get changed at all.
    Could you please tell me what I am doing wrong?

    Code (CSharp):
    1. public async void begin() {
    2.       Debug.Log($"The hidden join code is {joinCode}");
    3.       LobManager.assignLobby(lobby);
    4.  
    5.       var x = await Lock();
    6.  
    7.       Debug.Log($"Privated {lobby.IsPrivate}"); //false
    8.       Debug.Log($"Locked {lobby.IsLocked}"); //false
    9.  
    10.       networkManager.SceneManager.LoadScene("Game Board", LoadSceneMode.Single);
    11.     }
    12.  
    13.     private IEnumerator Lock() {
    14.       Debug.Log("Locking");
    15.  
    16.       UpdateLobbyOptions updateOptions = new UpdateLobbyOptions() {
    17.         IsPrivate = true,
    18.         IsLocked = true
    19.       };
    20.  
    21.       Debug.Log($"update IsPrivate = {updateOptions.IsPrivate}"); //true
    22.       Debug.Log($"update IsLocked = {updateOptions.IsLocked}"); //true
    23.  
    24.       var t = Lobbies.Instance.UpdateLobbyAsync(lobby.Id, updateOptions);
    25.  
    26.       Debug.Log($"t.IsCompleted {t.IsCompleted}"); //false
    27.  
    28.       yield return new WaitWhile(() => t.IsCompleted == false);
    29.  
    30.       Debug.Log($"update IsPrivate = {updateOptions.IsPrivate}"); //true
    31.       Debug.Log($"update IsLocked = {updateOptions.IsLocked}"); //true
    32.       Debug.Log($"t.IsCompleted {t.IsCompleted}"); //true
    33.       Debug.Log($"Finish with private = {lobby.IsPrivate}"); //false
    34.       Debug.Log($"Finish with lock {lobby.IsLocked}"); //false
    35.     }
     
  2. RobustKnittedTortoise

    RobustKnittedTortoise

    Unity Technologies

    Joined:
    Dec 10, 2018
    Posts:
    16
    Lobbies.Instance.UpdateLobbyAsync(lobby.Id, updateOptions);
    actually returns a task that will contain a new instance of the lobby with the updates applied when complete. Right now you are not observing the result of the task. The easiest was to do this would be update your Lock method to return async Task and get the new lobby by awaiting the update lobby call:
    Code (CSharp):
    1. private async Task Lock() {
    2.       Debug.Log("Locking");
    3.       UpdateLobbyOptions updateOptions = new UpdateLobbyOptions() {
    4.         IsPrivate = true,
    5.         IsLocked = true
    6.       };
    7.       Debug.Log($"update IsPrivate = {updateOptions.IsPrivate}");
    8.       Debug.Log($"update IsLocked = {updateOptions.IsLocked}");
    9.       var updatedLobby = await Lobbies.Instance.UpdateLobbyAsync(lobby.Id, updateOptions);
    10.  
    11.       Debug.Log($"Finish with lock {updatedLobby.IsLocked}");
    12.       // update the lobby member to be the updated lobby
    13.       lobby = updatedLobby;
    14.     }
     
  3. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    Andy's solution above is probably the easiest, but if you want to keep the Enumerator pattern you're using you _may_ be able to do so by using
    t.Result
    after the yield return statement instead of
    lobby
    which is the same thing as Andy is suggesting, i.e. observing the result of the task.

    I'm not super clear around how the IEnumerator from your Lock method is being used, so I don't know for sure if this'll do what you want, but in most Async Enumerator patterns that should do the trick.
     
  4. GlomyShadow

    GlomyShadow

    Joined:
    Jan 8, 2022
    Posts:
    4
    Alright, Thanks you two for the help!