Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question A bit confused about how relay works with lobby, why must we join with relay after joining lobby?

Discussion in 'Lobby' started by luniac, Nov 10, 2023.

  1. luniac


    Jan 12, 2011
    So i got this code to join a lobby by code:
    Code (CSharp):
    1. public async Task JoinPrivateLobby()
    2. {
    3.     try
    4.     {
    5.         currentLobby = await LobbyService.Instance.JoinLobbyByCodeAsync(privateMatchJoinCode);
    6.         pollForUpdatesTimer.Start();
    8.         string relayJoinCode = currentLobby.Data[k_keyJoinCode].Value;
    9.         JoinAllocation joinAllocation = await JoinRelay(relayJoinCode);
    12.         NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(joinAllocation, connectionType));
    14.         NetworkManager.Singleton.StartClient();
    I'm not really understanding why with have to do relay related code after we already joined the lobby by code.

    If we have to do allocation with relay, do stuff with server data and unityTransport, etc,
    whats the point of even joining a lobby by code?

    Is the lobby simply a way to organize players together in rooms, and the actual interactivity is all done with relay basically?
    So the lobby system is basically very barebones type thing?
  2. skizerzz


    May 14, 2021
    You kind of hit the nail on the head. The Lobby system is a standalone system that does have relatively easy integration with Relay. An example of integration would be letting players fill into a Lobby, and when the Lobby host decides to launch the game, they start a Relay allocation, call StartHost() on their NetworkManager Singleton, and when they receive the Relay joincode, they update the Lobby Data with the joincode. Have the Clients listen for Lobby Events using Lobbies.Instance.SubscribeToLobbyEventsAsync(LobbyId, callbacks); callbacks being a LobbyEventCallbacks object you can apply listening functions to. And when the event occurs that the Lobby.Data is changed, check to see if the changes includes a Relay joincode, and if it does, join the allocation with the joincode.
    luniac likes this.