Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Problem joining a private lobby through LobbyCode (JoinLobbyByCodeAsync)

Discussion in 'Lobby' started by KadaXuanwu, May 28, 2022.

Thread Status:
Not open for further replies.
  1. KadaXuanwu

    KadaXuanwu

    Joined:
    Oct 26, 2019
    Posts:
    9
    Hello there,
    In my project, I was able to successfully create and join lobbies with 'QuickJoinLobbyAsync'. But then I wanted to create a private lobby (which seems to succeed), then join it with 'JoinLobbyByCodeAsync' together with the accoring lobby code retrieved from 'lobby.LobbyCode'.
    Everytime I try to join the private lobby, I get a ValidationError (16000): request failed validation, and it tells me that it cannot find a lobby.

    Code (CSharp):
    1. try
    2.             {
    3.                 // Client looking for a lobby
    4.  
    5.                 // Add options to the matchmaking (mode, rank, etc..)
    6.                 JoinLobbyByCodeOptions options = new JoinLobbyByCodeOptions();
    7.  
    8.                 // Join a private lobby
    9.                 Lobby lobby = await Lobbies.Instance.JoinLobbyByCodeAsync(uiTextJoinPrivate.text.ToUpper(), options);
    10.  
    'uiTextJoinPrivate' is where I enter the code retrieved from the host's view.
    I have activated Relay and Lobby in the dashboard and linked the project correctly.

    Here is how I start the lobby:
    Code (CSharp):
    1. try
    2.             {
    3.                 // Create RELAY object
    4.                 Allocation allocation = await Relay.Instance.CreateAllocationAsync(5);
    5.                 _hostData = new RelayHostData
    6.                 {
    7.                     Key = allocation.Key,
    8.                     Port = (ushort)allocation.RelayServer.Port,
    9.                     AllocationID = allocation.AllocationId,
    10.                     AllocationIDBytes = allocation.AllocationIdBytes,
    11.                     ConnectionData = allocation.ConnectionData,
    12.                     IPv4Address = allocation.RelayServer.IpV4
    13.                 };
    14.  
    15.                 string lobbyName = $"{uiTextPlayerName.text}'s Lobby";
    16.                 int maxPlayers = 6;
    17.                 CreateLobbyOptions options = new CreateLobbyOptions();
    18.                 options.IsPrivate = true;
    19.  
    20.                 Database.lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
    21.  
    22.                 // Save Lobby ID for later uses
    23.                 _lobbyId = Database.lobby.Id;
    24.  
    25.                 Debug.Log("Created lobby: " + Database.lobby.Id);
    26.  
    27.                 // Heartbeat the lobby every 15 seconds.
    28.                 StartCoroutine(HeartbeatLobbyCoroutine(Database.lobby.Id, 15));
    29.  
    30.                 // Now that RELAY and LOBBY are set...
    31.  
    32.                 // Set Transports data
    33.                 NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(
    34.                     _hostData.IPv4Address,
    35.                     _hostData.Port,
    36.                     _hostData.AllocationIDBytes,
    37.                     _hostData.Key,
    38.                     _hostData.ConnectionData);
    39.  
    40.                 // Finally start host
    41.                 if (NetworkManager.Singleton.StartHost())
    42.                 {
    43.                     Debug.Log("host started");
    44.                 }
    45.                 else
    46.                 {
    47.                     Debug.Log("host failed");
    48.                 }
    49.  
    50.                 Debug.LogError(Database.lobby.LobbyCode);
    51.                 //SceneManager.LoadScene("LobbyRoom");
    52.             }
    53.             catch (LobbyServiceException e)
    54.             {
    55.                 Console.WriteLine(e);
    56.                 throw;
    57.             }
    58.         }
    59.  
    60.         IEnumerator HeartbeatLobbyCoroutine(string lobbyId, float waitTimeSeconds)
    61.         {
    62.             var delay = new WaitForSecondsRealtime(waitTimeSeconds);
    63.             while (true)
    64.             {
    65.                 Lobbies.Instance.SendHeartbeatPingAsync(lobbyId);
    66.                 Debug.Log("Lobby Heartbit");
    67.                 yield return delay;
    68.             }
    69.         }
    70.  
    'Database' is just a class with static members to store some values for other scenes.

    Thanks for any advice!
     
  2. RobustKnittedTortoise

    RobustKnittedTortoise

    Unity Technologies

    Joined:
    Dec 10, 2018
    Posts:
    16
    This looks similar to an issue we have seen before where certain input methods contained some extra characters in addition to the 6 character lobby code (even a sneaky vertical tab or hidden character!).

    The last time I saw this issue, it was related to TextMeshPro and using:
    TMPro.<TMP GUI element>.text
    instead of:
    TMPro.<TMP Input element>.text

    I would double check exactly what value is being sent as the lobby code by logging the value being passed in as the lobby code.
     
  3. KadaXuanwu

    KadaXuanwu

    Joined:
    Oct 26, 2019
    Posts:
    9
    Thank you a lot, this actually fixed my problem. There was another character appended to the string, so the code was wrong every time.

    I changed the UI element from "TextMeshProUGUI" to "TMP_InputField", which fixed it.
     
    Last edited: Jun 6, 2022
    Scarafone and mrstruijk like this.
  4. emilyryan

    emilyryan

    Unity Technologies

    Joined:
    Nov 22, 2019
    Posts:
    129
    We are glad to hear this resolved your issue! We are going to close out this post, but feel free to create a new one if you have any other questions. Thanks for your time and I hope you have a great day!
     
Thread Status:
Not open for further replies.