Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Can't connect Netcode for Entities to Lobby and Relay

Discussion in 'Multiplayer' started by aducceschi, Jun 17, 2022.

  1. aducceschi

    aducceschi

    Joined:
    Feb 9, 2022
    Posts:
    29
    Hi, I've recently been able to connect multiple instances of a game that uses Entities by using the tutorial provided by Unity. I wanted then to connect users by using the Relay and Lobby systems. So I implemented the Lobby system by examining and reusing the HelloWorldSample functions.

    I managed to connect two different PCs with the Lobby system and managed to join the two instances in the same lobby.

    So here is the problem:

    I can't make the Netcode for Entities system use the Lobby system at all. They don't seem to be automatically connected or anything.

    And I can't find any sort of tutorial or sample project in which the two things are connected. The only two samples that Unity provided show:
    1. How to connect the Netcode for Gameobjects to the Lobby system.
    2. How to connect two PCs using the direct IP system for Netcode for Entities.

    Does anyone know how to connect both systems? Any help will be greatly appreciated.
     
  2. krooq

    krooq

    Joined:
    Jan 30, 2013
    Posts:
    175
    aducceschi likes this.
  3. aducceschi

    aducceschi

    Joined:
    Feb 9, 2022
    Posts:
    29
    Thanks! Right now I left the Multiplayer part of my game on hold until Unity creates some documentation on Netcode for Entities + Relay. But I'll check out the link you gave me!
     
  4. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    36
    [edit] I missread your post, maybe because I had just woken up lol But at the end of the day I was doing exactly what you wanted to do, read my last message, it has the code that connects to the lobby and uses the relay to connect the players. I hope it helps. [/edit]

    Aducceshi, I think the best way to start are the demos and the Lobby and Relay docs. I just start implementing the a lobby system with auto connect and had some issues (here is thread opened yesterday: https://forum.unity.com/threads/lobby-quick-join-is-not-working.1312230/)

    The path I am following is:

    1. Installed the "Simple Relay Sample'' from the relay package and played around with it, its a really one script sample example, very good to understand the flow of the relay system with the docs support: https://docs.unity.com/relay/introduction.html

    2. I need a quick join system without the needing of passing codes to the clients, so I implemented a quick lobby join (code attached, not ready yet, but I hope it can help you)

    3. To the lobby system I am using the "Lobby Hello World" sample from the lobby package, its another very simple one script sample and is very good to understand the flow along side with the lobby documentation: https://docs.unity.com/lobby/unity-lobby-service-overview.html

    4. Now I am trying to undertand how to integrate the lobby flow with the NetworkManager, and in a near future I want to do it with a dedicated server approach. As soon as I realize how to put all these pieces toguer I will let you know, probably today I will get it.

    I had some issues using the QuickJoinLobbyAsync so went to an approach using the QueryLobbiesAsync, and its quite working lol as described in my Lobby Quick Join is not working? Thread.

    I think the next step is the Boss Room sample, but I do have the time now to see all the demo, and just want the quick connection to do some initial tests.

    I hope this can help you somehow, and another important documentation resource is this: https://docs-multiplayer.unity3d.com/netcode/current/about

    I will keep posting some updates in my quick connection thread.

    Best Regards
     

    Attached Files:

    Last edited: Jul 22, 2022
    aducceschi likes this.
  5. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    36
    Humm, I just realized how to use the Lobby + Relay API

    For some reason I thought the Lobby API would be auto integrated with the Relay system, now I am really getting to the point of your question.

    I did a very basic integration with Lobby + Relay + NetworkManager, code attached.

    The flow I am using is:

    1. Initialize Unity Services
    2. Sign In Anonymously
    3. Here we have two different paths, one for the host player and one for the client player
    3.1 Host Player
    3.1.1 The host will be the one that creates the lobby and the relay allocation
    3.1.2 The lobby and the relay allocation are created if the player connecting does not find a named lobby
    3.1.3 If the queried lobby does not exist the player creates it, but before creating the lobby the player creates a relay allocation and gets its join code. With the join code the player creates the lobby passing the Join code into the lobby data. This lobby data can be accessed for the client players
    3.1.4 After creating the lobby and the relay allocation the player starts the NetworkManager as a host​
    3.2 Client Player
    3.2.1 the client player searches for the named lobby, as it was already created by the host the player just need to connect to the lobby
    3.2.2 After connecting to the lobby the player gets the relay join code and joins the relay allocation
    3.2.3 After joining the relay allocation the player starts the NetworkManager as a client​

    That's it, maybe my explanation is confusing, but the code is very simple.

    It's quite working, I am just having a "player is already a member of the lobby" when joining as client player, that is why the authentication service is using the same player, I will look for a way to auth with too different players and it should work. I will keep you posted.
     

    Attached Files:

    aducceschi likes this.
  6. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    36
    Its working!!! I just changed the InitializeUnityServicesAsync method. As I am using ParrelSync, I just use the argument to set the player profile. Yes, I just discovered this ProfileManagement thing https://docs.unity.com/authentication/ProfileManagement.html

    Code (CSharp):
    1. public async Task InitializeUnityServicesAsync()
    2.     {
    3.         var options = new InitializationOptions();
    4.  
    5.         if (ClonesManager.IsClone())
    6.         {
    7.             options.SetProfile(ClonesManager.GetArgument());
    8.         }
    9.  
    10.         await UnityServices.InitializeAsync(options);
    11.     }
    Now I will do some remote tests and organize my code lol
     

    Attached Files:

    aducceschi likes this.
  7. aducceschi

    aducceschi

    Joined:
    Feb 9, 2022
    Posts:
    29
    Thank you so much for the info and script!! Tomorrow I'll try to implement it and I'll tell you if it works for me!
     
    JJunior likes this.
  8. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    36
    Aducceshi, I just changed the code to use the quick join, it's now working after I changed the code to use different profiles from the different clients I was using to test.
     

    Attached Files:

    aducceschi likes this.
  9. aducceschi

    aducceschi

    Joined:
    Feb 9, 2022
    Posts:
    29
    Hey, I tried your code but I can't even get it to compile because there's some packages missing apparently.
    It says it requieres Unity.NetCode.Transports.UTP. And it also requires a "NetworkManager" and a "Transport" Classes. Are you using Unity Netcode for Entities? Because this is stuff from the Netcode for GameObjects. And I cannot install the Gameobjects package because it conflicts with the Entities package.
    I get this errors if I try to install both packages at once:
    Screenshot 2022-07-30 at 12.05.24.png

    Screenshot 2022-07-30 at 11.59.29.png
     
  10. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    36
    Yes, I am using the Netcode for GameObjects :S
     
    aducceschi likes this.