Search Unity

Question Issues Connecting Client Build to Server in Editor

Discussion in 'NetCode for ECS' started by JohnnyTurbo, Oct 4, 2023.

  1. JohnnyTurbo


    May 17, 2017
    Hello! I feel like I'm probably missing something super basic here, but I am trying to test a NFE project having the editor act as a host and a build connect to it as a client.

    Right now in the editor I can start the server and join the game via the editor, but cannot join via a client build. Similarly, I can start the server and join the game through a build, but the editor cannot join that game.

    Upon further investigation, it doesn't look like the standalone client is receiving a network ID from the server (cannot find an entity with that component in the client world). Also I can see that in the Multiplayer PlayMode tools window, I can see two clients as "connected" but only one in game.


    My world setup code is basically the same as the example projects, but here is it for reference.

    Code (CSharp):
    1. private void StartServer()
    2. {
    3.     var server = ClientServerBootstrap.CreateServerWorld("Turbo Server World");
    5.     DestroyLocalSimulationWorld();
    6.     var ep = NetworkEndpoint.AnyIpv4.WithPort(Port);
    7.     {
    8.         using var drvQuery = server.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
    9.         drvQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(ep);
    10.     }
    11. }
    13. private void StartClient()
    14. {
    15.     var client = ClientServerBootstrap.CreateClientWorld("Turbo Client World");
    17.     SceneManager.LoadScene(1);
    18.     DestroyLocalSimulationWorld();
    20.     var ep = NetworkEndpoint.Parse(Address, Port);
    21.     {
    22.         using var drvQuery = client.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
    23.         drvQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Connect(client.EntityManager, ep);
    24.     }
    25. }
    27. private static void DestroyLocalSimulationWorld()
    28. {
    29.     foreach (var world in World.All)
    30.     {
    31.         if (world.Flags == WorldFlags.Game)
    32.         {
    33.             world.Dispose();
    34.             break;
    35.         }
    36.     }
    37. }

    Any thoughts/suggestions are much appreciated!
    Antypodish likes this.
  2. CMarastoni


    Unity Technologies

    Mar 18, 2020
    It may be the client is failing the protocol authentication and as such become disconnected immediately.

    Could you try to enable DynamicAsseblyList in the RPC collection. Editor and client has different RPC set so they fail to connect without enabling this.

    /// <summary>
    /// This allows sending RPCs between a stand alone build and the editor for testing purposes in the event when you finish this example
    /// you want to connect a server-client stand alone build to a client configured editor instance.
    /// </summary>
    [WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ServerSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
    public partial struct SetRpcSystemDynamicAssemblyListSystem : ISystem
    public void OnCreate(ref SystemState state)
    SystemAPI.GetSingletonRW<RpcCollection>().ValueRW.DynamicAssemblyList = true;
    state.Enabled = false;
    JohnnyTurbo likes this.
  3. JohnnyTurbo


    May 17, 2017
    Thank you so much for the reply @CMarastoni !

    That didn't end up being the solution, but it was helpful in figuring out what the issue was. Long story short, after adding this system I noticed I could get a build client player to spawn in game, but things weren't synching in real-time between editor and build. This led me to enable "Run in Background" in my player settings and all is working as expected!

    I confirmed that with Run in BG enabled, I no longer need the system you provided in the project.

    Thanks again for the help and I knew it was going to be something simple
  4. CMarastoni


    Unity Technologies

    Mar 18, 2020
    Oh yeah, that can also be the case. I was supposing the flag was already being set.
    JohnnyTurbo likes this.