Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

ForEach single Entity case

Discussion in 'Data Oriented Technology Stack' started by jooleanlogic, Feb 10, 2019.

  1. jooleanlogic

    jooleanlogic

    Joined:
    Mar 1, 2018
    Posts:
    327
    Is it possible to get a ForEach delegate that just takes an Entity?

    In my use case, my ComponentGroup just contains a single ISystemStateComponent tag that I'm going to remove in the loop. You can't ref zero-sized components though so I've had to add Position just as a placeholder.
    Code (CSharp):
    1. // This is all I need
    2. ForEach( (Entity entity) => {
    3.     ...
    4. }, GetComponentGroup(typeof(MySystemStateTagComponent)) );
    5.  
    6. // But I have to do this
    7. ForEach( (Entity entity, ref Position pointlessComponent) => {
    8.     ...
    9. }, GetComponentGroup(typeof(MySystemStateTagComponent), typeof(Position) );
    I assume this is because ForEach takes an optional null ComponentGroup so you need identifying components in the delegate in order to create one?

    I added such a use case in ForEachIterator.generated.cs and it hasn't blown up so far.
    I just stripped the component and made ComponentGroup non-nullable-optional.
    Code (CSharp):
    1.  
    2. protected delegate void F_E(Entity entity);
    3.  
    4. unsafe protected void ForEach(F_E operate, ComponentGroup group)
    5. {
    6. #if ENABLE_UNITY_COLLECTIONS_CHECKS
    7.     EntityManager.m_InsideForEach++;
    8.     try
    9. #endif
    10.     {
    11.         var entityType = GetArchetypeChunkEntityType();
    12.  
    13.         using (var chunks = group.CreateArchetypeChunkArray(Allocator.TempJob))
    14.         {
    15.             foreach (var chunk in chunks)
    16.             {
    17.                 var length = chunk.Count;
    18.  
    19.                 var entityArray = chunk.GetNativeArray(entityType);
    20.                 for (int i = 0; i < length; ++i)
    21.                     operate(entityArray[i]);
    22.             }
    23.         }
    24.     }
    25. #if ENABLE_UNITY_COLLECTIONS_CHECKS
    26.     finally
    27.     {
    28.         EntityManager.m_InsideForEach--;
    29.     }
    30. #endif
    31. }
     
    Last edited: Feb 10, 2019
    mkracik and Guerro323 like this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,631
    It will be in the next release.
     
    mkracik, MostHated and FROS7 like this.