Search Unity

Feedback Summary: Rules and behavior of Systems in Unity ECS

Discussion in 'Entity Component System' started by friflo, Dec 18, 2020.

  1. friflo

    friflo

    Joined:
    Jan 16, 2016
    Posts:
    10
    For my project I had the need to run - Update() - the same system multiple times in one player loop.
    To do this it was necessary to unterstand the restrictions and behavior of the System implementation in Unity ECS.

    This information may be useful for others - so I write them down in this summary:
    1. Each System (class) is registered exactly once in a World by an instance of the system class by calling World.GetExistingSystem<>() or World.GetOrCreateSystem<>().
      This is somehow similar to the "behavior pattern" used by MonoBehavior and GetComponent<>()
      • A system cannot instantiated manually. This is always done implicit by Unity.Entities.TypeManager.ConstructSystem()
        So initialization of a system need to be done in OnCreate() and not in its constructor.
      • Each World have only one instance of a system (not 2 ore more).
      • Instantiation of a system manually with new MySystem() result in an error: "InvalidOperationException: object is not initialized or has already been destroyed"
    2. Typically a system (instance) is added to a ComponentSystemGroup once by using the attribute [UpdateInGroup], [UpdateAfter]'s and [UpdateBefore]'s.
      To avoid adding a system (instance) automatically to a system group [DisableAutoCreation] is used.
      • Adding a system via the attributes enables showing them up in the Entity Debugger.
    3. The same system (reflected by its single instance in the world) can be run multiple times by calling Update() on the system directly or indirectly by calling Update() on a the system group containing the system.
      • A System executed directly with Update() will not show up in the Entity Debugger (right now).
     
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    There are many ways you can update one system multiple times.
    1. For compile time const update count, you can simple inherent the original system and register it after the original one any time you want.
    2. Schedule the same IJobChunk multiple times.
    3. if it's a simple job you can simply for loop in the job and update multiple times.
     
    friflo likes this.