Search Unity

Question Best way to get SubScenes in late created Client Server worlds?

Discussion in 'NetCode for ECS' started by joshrs926, Oct 17, 2023.

  1. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    112
    I have a custom ClientServerBootstrap that just creates 1 local world. I then connect to Relay and use its allocations to create a new NetworkDriverConstructor and then I create the Client and Server worlds. See this thread for why I’m doing it this way.

    One problem with this is that any SubScenes I have in my normal Unity Scene will get instantiated into the initial local world but not the late client and server worlds. In the NetworkCube tutorial you create a Subscene and it gets loaded into both client and server worlds which are created by the custom ClientServerBootsrap. I really like that. Is it possible to make this happen for late created worlds? If not, then what’s the best way to get the SubScenes loaded into late created client and server worlds?
     
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    If you take a look a the NetcodeSamples (in particular Frontend.cs) we have a similar flow as well.

    In general, the default world must be destroyed (or you should have a world that does not have either a SceneSystem nor a presentation group) and the two NetcodeWorld created.

    The default world should be destroyed and both Client and Server worlds should be created before the game scene or the scene that contains the sub-scene is going to loaded.

    Otherwise, you need to do that manually yourself, by getting all the SubScene behaviour in the scene, get the SceneGUID and issue a load to the SceneSystem in each of the world.

    Enable/Disable the sub-scene can also work, but attenuation that when you disable the gameobject, the sub-scene is unloaded from all world first.

    The order of creation and destruction of worlds is particularly important. That because of some ref-counting used for tracking GameObject scene that cause the current loaded scene to be unloaded when a world is destroyed if that ref-count go to 0.
     
    joshrs926 likes this.
  3. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    112
    Thanks for the reply. So it sounds like a good pattern might be
    1. Start the game in a normal-scene without any sub-scenes in it
    2. Load the client and server entity-scenes
    3. Destroy the initial default entity-scene
    4. Load a normal-scene that contains sub-scenes in it. This will cause the sub-scenes to get loaded in all currently existing worlds
    Does that sound right? I had thought about this approach, but how would I get a notification that the sub-scenes were loaded? I'm sure there's a callback for async loading the normal-scene, but does that callback get invoked before or after the sub-scenes finish loading?