Search Unity

Question How to integrate Relay and Lobby

Discussion in 'Lobby' started by geronimo_desenvolvimentos, Mar 5, 2022.

  1. geronimo_desenvolvimentos

    geronimo_desenvolvimentos

    Joined:
    May 24, 2013
    Posts:
    17
    In Unity's docs (https://docs.unity.com/lobby/relay-integrations.html) they say: "To enable relay integration with the Lobby service, you must set the AllocationID for each player whenever a player is in a lobby and is using a relay. The AllocationID should be the relay allocation ID returned by the JoinRelayAsync API call.". I could not understand what they meant and I couldn't find the relevant code in their samples.

    How do I integrate Unity Relay and Unity Lobby? What does Set the AllocationId with the result of JoinRelayAsync means? Set where? Set how? Is there any minimal code sample showing how to do that?
     
  2. bartj-unity

    bartj-unity

    Unity Technologies

    Joined:
    Sep 9, 2021
    Posts:
    26
    Hi there,

    It looks like those docs are out-of-date - we'll get those updated. The function referenced should actually be "CreateAllocationAsync" instead.

    You mentioned samples, but in case you weren't using this, you can take a look at the Lobby-Relay sample for a detailed example of how to integrate Lobby with Relay. Specifically, take a look at this line for an example of how that function gets used - the "Allocation" object that gets returned contains an allocation ID. You can then see that allocation ID eventually get set for a player within a lobby on this line.
     
  3. geronimo_desenvolvimentos

    geronimo_desenvolvimentos

    Joined:
    May 24, 2013
    Posts:
    17
    Thank you, I will go back to this part of the project soon and give you feedback
     
  4. geronimo_desenvolvimentos

    geronimo_desenvolvimentos

    Joined:
    May 24, 2013
    Posts:
    17
    There is one thing that I could not understand: That the host sets the allocationId that comes from Relay.Instance.CreateAllocationAsync is clear to me now. But what about the clients? How they can know the AllocationId beforehand?
    My flow is:
    1) Alice tries to find an open lobby using Lobbies.Instance.QuickJoinLobbyAsync(options).
    2) Alice finds no open lobby. Alice create it's own relay and creates it's lobby using CreateAllocationAsync and use the allocationId and the JoinCode to set it's own lobby, allocationId goes to the allocationId in the Player and JoinCode to connectionInfo. So now I have a Relay with Alice as host and a Lobby with Alice with enough data for people to join the match.
    3) Bob tries to find an open lobby like Alice did, and finds Alice's lobby. Bob uses the joinCode in Alice's connectionInfo and join the Relay, then NGO finishes it's magic and Bob and Alice are playing together.

    If am i to use allocationId, how can Bob knows Alice's Relay's allocationId before knowing that Alice exists, because Bob isn't in the Lobby yet so can't see the value? Or Bob don't have to set allocationId in this step here:

    Code (CSharp):
    1.         QuickJoinLobbyOptions options = new QuickJoinLobbyOptions();
    2.         options.Player = new Player(
    3.             id: AuthenticationService.Instance.PlayerId
    4.             );
    5.         options.Filter = new List<QueryFilter>()
    6.                     {
    7.                         new QueryFilter(
    8.                             field: QueryFilter.FieldOptions.MaxPlayers,
    9.                             op: QueryFilter.OpOptions.GE,
    10.                             value:"2")
    11.                     };
    12.         Lobby lobby = await Lobbies.Instance.QuickJoinLobbyAsync(options);
     
  5. UnityKip

    UnityKip

    Unity Technologies

    Joined:
    Nov 15, 2021
    Posts:
    36
    It will not be possible for Bob to find this information prior to connecting to Alice's Lobby unless it is provided out-of-band from the Lobby service (e.g. a private message through Discord). For a client connection like Bob's, the client should first connect to the Lobby and then check the AllocationId from the Lobby data.

    Once your clients have connected to the Lobby, you will need to handle joining the Relay and setting the AllocationId. You can see an example of this in the Sample Game Lobby. Please also note the OnLobbyChange() function which demonstrates a way for clients to automatically identify a join code being added or changed in a Lobby. This would handle the case where Alice creates a Lobby without a Relay and adds it after other players have connected to the Lobby or after a host migration.

    Let me know if this helps and if you have any other questions.

    -Kip
     
  6. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    171
    Hi @don_geronimo_hue, did you manage to get relay-lobby integrations to work? I'm not quite sure why, but it just isn't working for me for some reason, and I've followed the example projects (Boss-Room and Sample-Game-Lobby) as close as possible! Thanks.
     
  7. Mid_D_Man

    Mid_D_Man

    Joined:
    Jan 26, 2022
    Posts:
    22
    Hello can anyone please help me I've been trying netcode for gameobjcts, an I still can't figure out how I would go about adding random team selection in my game so that as more people join the game lobby the teams get filled up and once one team is filled the other gets filled up to