Search Unity

How to create custom world with specific ComponentSystems

Discussion in 'Entity Component System' started by emreCanbazoglu, Mar 25, 2018.

  1. emreCanbazoglu

    emreCanbazoglu

    Joined:
    Jan 31, 2012
    Posts:
    15
    In the documentation:
    https://github.com/Unity-Technologi...master/Documentation/content/ecs_in_detail.md

    It is said that
    However, the links provided in the documentation are dead. Are there any examples that show how to create custom worlds and add spesific systems to that world?
     
  2. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    @emreCanbazoglu we don't have any public examples that use multiple worlds yet, and the API documentation still has to be completed.

    That said, creating and using new worlds is quite simple:


    var newWorld = new World("My New World");
    var entityManager = newWorld.GetOrCreateManager<EntityManager>();

    // ... do what u want

    newWorld.Dispose();


    We create a default World, that is accessible via World.Active (you can change it by assigning to this property as you see fit), and you can access all the worlds via World.AllWorlds.

    Once the manager is created (you can create multiple of them), you can call ScriptBehaviourUpdateOrder.UpdatePlayerLoop to inject them in the player loop so they get ticked regularly. Or you can manually tick them as you see fit.

    But yes, we definitely need a good example on how to use the World API and how to deal with multiple worlds.
     
    deus0 and T-Zee like this.
  3. sahildhanju

    sahildhanju

    Joined:
    Mar 27, 2018
    Posts:
    6
    @GabrieleUnity Could you show how to tick two custom created worlds at the same time? Currently it seems like World.Active is the only world that gets automatically ticked in the player loop even if you have called ScriptBehaviourUpdateOrder.UpdatePlayerLoop

    Edit: It works. (Dunno what changed :D). The question now is how do you know which world you are in when in OnUpdate() inside a system?
     
    Last edited: Apr 5, 2018
  4. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    Can't you check with `World.Active`?
     
  5. sahildhanju

    sahildhanju

    Joined:
    Mar 27, 2018
    Posts:
    6
    @GabrieleUnity as far as I can tell, nothing is changing what World.Active points to during execution. So if you have two worlds, and set World.Active to be one of those worlds, both systems will see World.Active as the same world.
     
  6. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    Isn't
    ComponentSystem.World
    set to the correct
    World
    instance?
     
  7. sahildhanju

    sahildhanju

    Joined:
    Mar 27, 2018
    Posts:
    6
    You're absolutely right, it does. Wasn't able to find that earlier.

    Something related is that when I have used ScriptBehaviourUpdateOrder.UpdatePlayerLoop to start ticking my worlds, I have to call the function with both worlds rather than one by one. If the eventually examples you all include can show that, that would be nice.