Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug System updates even with disabled component

Discussion in 'Entity Component System' started by Ayvaki, Dec 11, 2022.

  1. Ayvaki

    Ayvaki

    Joined:
    Dec 11, 2022
    Posts:
    1
    Hi folks! I created simple system with RequireForUpdate and I want to not update this system if there is zero enabled DisabledComponent. Is it possible for now?

    Code (CSharp):
    1. public struct DisabledComponent : IComponentData, IEnableableComponent
    2. {
    3.     public int Value;
    4. }
    5.  
    6. public partial struct TestSystem : ISystem
    7. {
    8.     public void OnCreate(ref SystemState state)
    9.     {
    10.         state.RequireForUpdate<DisabledComponent>();
    11.     }
    12.  
    13.     public void OnDestroy(ref SystemState state)
    14.     {
    15.     }
    16.  
    17.     public void OnUpdate(ref SystemState state)
    18.     {
    19.         UnityEngine.Debug.Log("updated");
    20.         foreach (var component in SystemAPI.Query<RefRW<DisabledComponent>>())
    21.         {
    22.             component.ValueRW.Value++;
    23.         }
    24.     }
    25. }
     
  2. Jonas_DM_

    Jonas_DM_

    Joined:
    Feb 28, 2019
    Posts:
    22
    I think it might be intentional.
    If RequireForUpdate would support Filtering on the Enableable types it would introduce a sync point because it'd need to complete any job that is working with the component.

    RequireForUpdate uses EntityQuery::IsEmptyIgnoreFilter under the hood (see SystemState::ShouldRunSystem).

    If you don't mind the sync point & think the performance of the game would benefit from it you can add this to the start of your OnUpdate():
    Code (CSharp):
    1. if (_requiredQuery.IsEmpty) return;
    I have that in some places in my codebase.
     
  3. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    106