Search Unity

Question How to create multiple world?

Discussion in 'Entity Component System' started by Selshas, Jan 29, 2023.

  1. Selshas

    Selshas

    Joined:
    May 14, 2021
    Posts:
    7
    In short, I'm testing several thing for ECS in one Unity project. I'd like to separate worlds by scenes so that they have their own responsibility for specific algorithm or technology.

    I know I can disable bootstrapping and create my own worlds by instantiating of World class and I'm responsible to address systems/system groups in manual. I have tried already and It seems not properly working. maybe it is not enough just to create world instance and set its update function into PlayerLoop. SubScene component just been broken, the systems executed OnCreate() but didn't run Update() and also didn't appear at System window.
    and just disabling bootstrapping detaches too much things that DOTS needs to properly work. I just need copies of default world setting.

    DOTS is still not manualized well, and every guide documents and videos are not explaining about this. or too old and obsolete. that's why I came here.

    there're many games which have different flow and logics by scenes. and I believe that separating worlds by their purposes is much intuitive than just enabling/disabling systems.
     
  2. redwren

    redwren

    Joined:
    Aug 2, 2019
    Posts:
    69
    Creating a new world is straightforward:

    Code (CSharp):
    1.  
    2. var world = new World("Custom World");
    3.  
    4. var systems = new [] { typeof(FooSystem), typeof(BarSystem) };
    5.  
    6. DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
    7.  
    8. ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(world);
    9.  
    This will create a world with FooSystem and BarSystem, plus the top level system groups (InitializationSystemGroup, SimulationSystemGroup, PresentationSystemGroup). It will update its systems every frame using the same rules and attributes as the default world.

    You don't need to disable automatic bootstrapping to do this. A custom bootstrapper just affects the default world initialization.

    There are many more advanced directions you can go with custom worlds, like reusing the default system list (
    DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default)
    ), manually updating it with
    world.Update()
    , copying entities between worlds, etc. Depends on what you need the world for.
     
    Selshas and UniqueCode like this.
  3. Selshas

    Selshas

    Joined:
    May 14, 2021
    Posts:
    7
    Thank you for the answer. I've used manual on/off sets till now, I'll try this.