Search Unity

How the demo manages to selectively enable only some System on some scene?

Discussion in 'Data Oriented Technology Stack' started by 5argon, May 17, 2018.

  1. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,490
    Probably overlooked something. I learned that Unity loads ALL system in the project automatically and run its OnUpdate automatically :

    https://github.com/Unity-Technologi...ster/Documentation/content/getting_started.md

    Take SpawnRandomInSphereSystem for example, I put a Debug.Log in the OnUpdate

    Code (CSharp):
    1. namespace Samples.Common
    2. {
    3.     public class SpawnRandomInSphereSystem : ComponentSystem
    4.     {
    5.         struct SpawnRandomInSphereInstance
    6.         {
    7.             public int spawnerIndex;
    8.             public Entity sourceEntity;
    9.             public float3 position;
    10.             public float radius;
    11.         }
    12.  
    13.         ComponentGroup m_MainGroup;
    14.  
    15.         protected override void OnCreateManager(int capacity)
    16.         {
    17.             m_MainGroup = GetComponentGroup(typeof(SpawnRandomInSphere), typeof(Position));
    18.         }
    19.  
    20.         protected override void OnUpdate()
    21.         {
    22.             UnityEngine.Debug.Log(nameof(SpawnRandomInSphereSystem));
    23.  
    And in a scene like "DungeonFirstExample" where spawning in circle is not needed, this system is not getting run OnUpdate even once. Where is the criteria? I searched for "SpawnRandomInSphereSystem" on the entire project and this file is the only place.

    I used EntityDebugger and see the system being greyed out with running:no also. Where in the project that we can turn a system into not running? In my game all systems I create is running and that would be bad if I have multiple scenes and a system intended for other scenes start running on its own.
     
  2. jooleanlogic

    jooleanlogic

    Joined:
    Mar 1, 2018
    Posts:
    332
    My understanding is that this is automatic behaviour if the ComponentGroup set contains no components.
    So the System won't run if there's no components to process. It's by design. I can't remember where I read that though.
     
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,490
    So the System automatically scans and know its own `ComponentGroup` variables? That's a bit "magic" like how Unity newbies wondering how Update() got called...

    So that means if the system does not contains any `ComponentGroup` variable it will always run without condition. Then by destroying the world after each scene I think it will also stop all unrelated system from running in the next scene.
     
  4. jooleanlogic

    jooleanlogic

    Joined:
    Mar 1, 2018
    Posts:
    332
    Just did a quick test and that seems to be the case yes. A system with no injections will always run though I don't know the exact criteria.
    I assume Unity has to process all Systems in your game but if a System is unrelated in that there are no matching component groups in the scene, then it just won't run the OnUpdate. It can't know when you might add a component which will require a currently unused System.
    There'll need to be some way to control whether non-injecting systems run in a given scene or not though.
     
    5argon likes this.
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,490
    Thank you for testing it out!

    Then I can just inject an empty component telling which scene it belongs to.. but if the ECS system is tidy enough I guess I would automatically not needing that.
     
  6. jooleanlogic

    jooleanlogic

    Joined:
    Mar 1, 2018
    Posts:
    332
    Turns out you can already selectively add Systems.
    There's a post on this here and some great source code for selective systems by poster element_wsc in that thread.
     
    5argon likes this.
unityunity