Search Unity

Question Problems with direct connection using Lobby

Discussion in 'Lobby' started by Krujero, Nov 28, 2022.

  1. Krujero

    Krujero

    Joined:
    May 15, 2021
    Posts:
    2
    Hello Developers,

    I'm having trouble creating a simple direct join connection using Lobby (and Relay).

    With the function CreateLobby(), I can create a Lobby and show it with a UI-Text.

    With the function FindLobby(), I can find and quickjoin to an open Lobby.

    Now I want to directly join a Lobby using an InputField and the JoinLobby()-Function but the error states:

    [Lobby]: LobbyNotFound, (16001). Message: lobby not found
    UnityEngine.Logger:LogError (string,object)

    I try to manually put the ID in the InputField and even Debug.Log the InputField to make sure it is the same ID but it just won't connect. And when I quickjoin, it does work. Here is the code:

    Code (CSharp):
    1. public async void CreateLobby()
    2.     {
    3.         Debug.Log("Creating a new lobby...");
    4.  
    5.         // External connections
    6.         int maxConnections = 1;
    7.         try
    8.         {
    9.             Allocation allocation = await Relay.Instance.CreateAllocationAsync(maxConnections);
    10.             _hostData = new RelayHostData
    11.             {
    12.                 Key = allocation.Key,
    13.                 Port = (ushort)allocation.RelayServer.Port,
    14.                 AllocationID = allocation.AllocationId,
    15.                 AllocationIDBytes = allocation.AllocationIdBytes,
    16.                 ConnectionData = allocation.ConnectionData,
    17.                 IPv4Address = allocation.RelayServer.IpV4
    18.             };
    19.  
    20.             // Retrieve JoinCode
    21.             _hostData.JoinCode = await Relay.Instance.GetJoinCodeAsync(allocation.AllocationId);
    22.  
    23.  
    24.             string lobbyName = "game_lobby";
    25.             int maxPlayers = 10;
    26.             CreateLobbyOptions options = new CreateLobbyOptions();
    27.             options.IsPrivate = false;
    28.            
    29.  
    30.             // Put the JoinCode in the lobby data, visible by every member
    31.             options.Data = new Dictionary<string, DataObject>()
    32.             {
    33.                 {
    34.                     "joinCode", new DataObject(
    35.                         visibility: DataObject.VisibilityOptions.Member,
    36.                         value: _hostData.JoinCode)
    37.                 },
    38.             };
    39.  
    40.             // Create the lobby
    41.             Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
    42.            
    43.             // Show the LobbyID in UI
    44.            
    45.             lobbyText.GetComponent<TMPro.TMP_Text>().SetText(lobby.Id);
    46.  
    47.             Debug.Log(_hostData.JoinCode);
    48.             Debug.Log("Created lobby: " + lobby.Id);
    49.  
    50.             // Heartbeat the lobby every 15 seconds.
    51.             stopheartbeat = false;
    52.  
    53.             if(stopheartbeat == false)  {
    54.                 StartCoroutine(HeartbeatLobbyCoroutine(lobby.Id, 15));
    55.             }
    56.             if(stopheartbeat == true) {
    57.                 StopCoroutine(HeartbeatLobbyCoroutine(lobby.Id, 15));
    58.             }
    59.  
    60.             NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(
    61.                 _hostData.IPv4Address,
    62.                 _hostData.Port,
    63.                 _hostData.AllocationIDBytes,
    64.                 _hostData.Key,
    65.                 _hostData.ConnectionData);
    66.  
    67.             NetworkManager.Singleton.StartHost();
    68.  
    69.         }
    70.         catch (LobbyServiceException e)
    71.         {
    72.             Console.WriteLine(e);
    73.             throw;
    74.         }
    75.     }
    76.  
    77. public async void FindLobby()
    78.     {
    79.             Debug.Log("Looking for a lobby...");
    80.  
    81.             try
    82.             {
    83.                
    84.                 QuickJoinLobbyOptions options = new QuickJoinLobbyOptions();
    85.  
    86.                 Lobby lobby = await LobbyService.Instance.QuickJoinLobbyAsync(options);
    87.  
    88.  
    89.                 Debug.Log("Joined lobby: " + lobby.Id);
    90.                 Debug.Log("Lobby Players: " + lobby.Players.Count);
    91.  
    92.                 string joinCode = lobby.Data["joinCode"].Value;
    93.  
    94.                 Debug.Log("Received code: " + joinCode);
    95.  
    96.                 JoinAllocation allocation = await Relay.Instance.JoinAllocationAsync(joinCode);
    97.  
    98.                 _joinData = new RelayJoinData
    99.                 {
    100.                     Key = allocation.Key,
    101.                     Port = (ushort)allocation.RelayServer.Port,
    102.                     AllocationID = allocation.AllocationId,
    103.                     AllocationIDBytes = allocation.AllocationIdBytes,
    104.                     ConnectionData = allocation.ConnectionData,
    105.                     HostConnectionData = allocation.HostConnectionData,
    106.                     IPv4Address = allocation.RelayServer.IpV4
    107.                 };
    108.  
    109.                 NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(
    110.                     _joinData.IPv4Address,
    111.                     _joinData.Port,
    112.                     _joinData.AllocationIDBytes,
    113.                     _joinData.Key,
    114.                     _joinData.ConnectionData,
    115.                     _joinData.HostConnectionData);
    116.  
    117.                 NetworkManager.Singleton.StartClient();
    118.             }
    119.             catch (LobbyServiceException e)
    120.             {
    121.                 Debug.Log("Cannot find a lobby: " + e);
    122.             }
    123.     }
    124.  
    125. public async void JoinLobby()
    126.     {
    127.              try
    128.         {
    129.             JoinLobbyByIdOptions options = new JoinLobbyByIdOptions();
    130.  
    131.             Lobby lobby = await LobbyService.Instance.JoinLobbyByIdAsync(joinCodeInputFieldText.GetComponent<TMPro.TMP_Text>().text, options);
    132.  
    133. //...nothing changes from here with FindLobby() after QuickJoinAsync()
    134.            }
    135. }
    136.  
    137.  
     
  2. RobustKnittedTortoise

    RobustKnittedTortoise

    Unity Technologies

    Joined:
    Dec 10, 2018
    Posts:
    16
    This had come up a few times here is the solution that worked for the last few users.
    Let me know if this resolves your issue and I will try to get this added to the FAQ or add a warning about it in the docs.
     
    Krujero likes this.
  3. Krujero

    Krujero

    Joined:
    May 15, 2021
    Posts:
    2
    OMG, Thank you so much, I was stuck for 6 hours figuring things out and it was a simple fix. For everyone, who has the same problem:

    1. Make sure to drag the right Game Object in the SerializeField (The Input Field and not the text of the input field)
    2. Use the GetComponent<TMPro.TMP_Inputfield>() like in my code below

    Code (CSharp):
    1. [SerializeField] private GameObject joinCodeInputField;
    2. ...
    3.  
    4. public async void JoinLobby()
    5.     {
    6.         Debug.Log("Looking for a lobby...");
    7.  
    8.         try
    9.         {
    10.             JoinLobbyByCodeOptions options = new JoinLobbyByCodeOptions();
    11.  
    12.             Lobby lobby = await LobbyService.Instance.JoinLobbyByCodeAsync(joinCodeInputField.GetComponent<TMPro.TMP_InputField>().text, options);
    13. ...}
    Thank you again!