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

Question Confused about EntityQueryBuilder's WithAll and WithAllRW

Discussion in 'Entity Component System' started by SUmmAgo, Sep 8, 2023.

  1. SUmmAgo

    SUmmAgo

    Joined:
    Oct 31, 2017
    Posts:
    3
    In my superficial understanding, EntityQuery is not responsible for the read-write access mode of the componentType. The componentType's dependencies are updated through processes such as SystemState.GetComponentTypeHandle<T>(isReadOnly). Logically, EntityQuery describes a specific collection. For example, it can be used by multiple IJobEntities, EntityQuery itself should not pay attention to the read and write modes of components.

    I found a proof, there is such a code:
    Code (CSharp):
    1. public struct SampleComponent : IComponentData { public int Value; }
    2. partial struct FooJobEntity : IJobEntity
    3. {
    4.      public void Execute(ref SampleComponent sample)
    5.      {
    6.          sample.Value += 1;
    7.      }
    8. }
    9. public partial class FooSystem : SystemBase
    10. {
    11.      EntityQuery customQuery;
    12.      protected override void OnCreate()
    13.      {
    14.          customQuery = GetEntityQuery(ComponentType.ReadOnly<SampleComponent>());
    15.      }
    16.      protected override void OnUpdate()
    17.      {
    18.          new FooJobEntity().ScheduleParallel(customQuery);
    19.      }
    20. }
    In the generated code, it will be detected whether customQuery has the componentType required by FooJobEntity, and the call chain will eventually call customQuery.HasComponentsRequiredForExecuteMethodToRun(componentsUsedInExecuteMethod of FooJobEntity).
    HasComponentsRequiredForExecuteMethodToRun source code is as follows:
    Code (CSharp):
    1. internal bool HasComponentsRequiredForExecuteMethodToRun(ref Span<ComponentType> componentsUsedInExecuteMethod)
    2. {
    3.     var requiredComponentsInCurrentQuery = new NativeHashMap<int, ComponentType.AccessMode>(8, Allocator.Temp);
    4.     var currentData = __impl->_QueryData;
    5.     for (int i = 0; i < currentData->RequiredComponentsCount; i++)
    6.     {
    7.         var component = currentData->RequiredComponents[i];
    8.         requiredComponentsInCurrentQuery.Add(component.TypeIndex, component.AccessModeType);
    9.     }
    10.     foreach (var executeComponent in componentsUsedInExecuteMethod)
    11.     {
    12.         if (!requiredComponentsInCurrentQuery.TryGetValue(executeComponent.TypeIndex, out ComponentType.AccessMode accessMode))
    13.             return false;
    14.         // No need to address `case ComponentType.AccessMode.Excluded`, since all component parameters used in
    15.         // `IJobEntity. Execute()` functions are required components.
    16.         switch (executeComponent.AccessModeType)
    17.         {
    18.             // If the Execute() function simply needs to read from Component A, then it does not matter whether
    19.             // the current query contains Component A with read-only or read-write access.
    20.             case ComponentType.AccessMode.ReadOnly:
    21.                 if (accessMode == ComponentType.AccessMode.Exclude)
    22.                     return false;
    23.                 continue;
    24.         }
    25.     }
    26.     requiredComponentsInCurrentQuery.Dispose();
    27.     return true;
    28. }
    This code shows that even though customQuery only requires Readonly of SampleComponent, but FooJobEntity requires ReadWrite of SampleComponent, there is still no problem.
    It seems like thant EntityQuery shouldn't be concerned with the read-write access mode of components, so I think EntityQueryBuilder's WithAllRW<> is redundant. Confused about this.
     
    Last edited: Sep 8, 2023
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    GetEntityQuery adds read or write dependencies to the system just like GetComponentTypeHandle etc
     
  3. SUmmAgo

    SUmmAgo

    Joined:
    Oct 31, 2017
    Posts:
    3
    thank you