Search Unity

Question Netcode: Proper way to make an "Offline" bootstrap and add networked systems later?

Discussion in 'NetCode for ECS' started by PhilSA, Feb 7, 2021.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    First I'd like to ask again if it would be possible to create a Netcode subforum like DOTS physics and DOTS animation has. Really helps organizing all the knowledge in one spot
    _________

    Now my question: I am making a game that plays offline by default, but there is an online mode. So I need the Netcode package in my project, but I don't want it to affect the default systems in my game

    I have read this section on the subject and copied the "ExampleBootstrap" in my project. However, this doesn't seem to totally work in my case, because I still see a lot of netcode-only systems/groups being created and I get NullReferenceExceptions in PhysicsWorldHistory at this highlighted line (I guess because the Client/Server simulation groups creation was prevented):

    And see how there are still tons of netcode-related systems being created in the default world:


    What makes certain Netcode system still be created with the "offline bootstrap" approach and others not?
     
    Last edited: Feb 7, 2021
    apkdev and Occuros like this.
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hi,
    First +1 on the sub forum.

    Regarding the actual question I have no idea but for a game that is both online and offline my first though would have been to make the game using netcode as an online game and for the offline mode make it "locally" online.
    I remember unity videos showcasing the netcode package were they showed several modes including ones that made the client and server world run on the same machine bypassing the transport layer.

    So instead of trying to "get out" of the netcode system would it not be better to work with them ?
    Would that not avoid code duplication/complexity of managing both modes ?
     
    Last edited: Aug 26, 2021
    Occuros likes this.
  3. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    260
    I am also curious about how to do this.

    I agree with this approach. "Client World" systems that you don't want to run when running the game in offline mode could check if a Server World and Client World exist and exit early if they are not in the server world.
     
  4. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    actually that's a very valid point and I think you're right that it's better to work with network systems all the time
     
  5. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    It's a win on the subforum finally :D
     
    MrEastwood and PhilSA like this.
  6. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    So, even though I agree that I should code my singleplayer systems with netcode systems in mind, I'll still need to create my client and server worlds at runtime. I think everything would be fine if it weren't for PhysicsWorldHistory spamming errors whenever ServerSimulationSystemGroup and ClientSimulationSystemGroup aren't yet created

    Error happens on this line:


    Is PhysicsWorldHistory missing null checks, or is it me who's missing something?
     
  7. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    Hi Phils,

    The PhysicsWorld history expect to be executed in a world where the ServerSimulationSystemGroup or the ClientSimulationSystemGroup exists. This is guarantee by the NetCodeBootstrap who provide correct systems to be created. Missing one of them is considered a critical error.

    For what I saw in your previous image, you are instantiating a lot of systems in the default world but some of the required ones are missing. Also looks like the ordering is also a little broken.
    ClientSimulationSystemGroup and ServerSimulationSystemGroup (along with others) need to be explicitly created (they are tagged with [DisableAutoCreation] attribute).
    The network bootstrap is designed to create different worlds for server and client, each with different set of systems. If you plan to have the client systems in the default world, you should be also disable the TickClientSimulation system, otherwise the ClientSimulationSystemGroup is going to be updated twice per frame.
     
    PhilSA likes this.
  8. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    The way we currently do manual client/server world creation is by using a custom bootstrap which creates the default world containing only systems explicitly marked as belonging in the default world (systems with
    [UpdateInWorld(UpdateInWorld.TargetWorld.Default)]
    ).

    When we want to start an offline mode we create both a client and server world using
    ClientServerBootstrap.CreateClientWorld(defaultWorld, "WorldName");
    and
    ClientServerBootstrap.CreateServerWorld(defaultWorld, "WorldName");


    You can see an example of what such a bootstrap looks like at https://docs.unity3d.com/Packages/com.unity.netcode@0.6/manual/client-server-worlds.html#bootstrap
     
    Krooq and PhilSA like this.
  9. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
  10. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    You do not have to create the client / server worlds right away - it should be fine to wait until you need them. We often create them from a menu of some kind.
    If the PhysicsWorldHistory system is in the ExplicitDefaultWorldSystems list that sounds like a bug.
    If you are not using lag compensation right now you can disable PhysicsWorldHistory by creating a singleton with a DisableLagCompensation component, we do that in most our samples.
     
    PhilSA likes this.
  11. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    Took me quite a while to figure out why my subscene wasn't rendering in the client world even thought it was loaded.
    Turned out it had something to do with the systems in the default world I had included all systems there too, which is wrong I guess.
    Might be worth adding a note about 'ExplicitDefaultWorldSystems' in the bootstrap docs and it's importance, I would have never figured it out without this thread.

    Personally I feel that the 'automatic' finding of systems is a big ol smell.
    I have always struggled with programming techniques that involve reflective black magic, e.g. annotations.
    I'm not sure how to solve this, I know that there is a certain amount of 'automatic' that you want with an engine but not being able to understand why something is happening is the Achilles heel of a programmer.

    Anyway, this was just feedback from a noob :)
     
  12. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    I would not disagree that the current automatic bootstrap does not sounds a proper and solid solution.
    NetCode one in particular is not what we would like it to be.
    There are already discussion about that topic, how to properly make system sorting/execution order more declarative etc etc.
     
    Occuros and Krooq like this.
  13. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    @CMarastoni Is there any plan to remove default world in next Netcode package or in future? But ConvertToClientServerEntity monobehavior still needs to work properly after this changes.
     
  14. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    No, default world for now is here to stay. ConvertToClientServerEntity is technically deprecated, but still available to use for certain scenario.