Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

ForEach single Entity case

Discussion in 'Entity Component System and C# Job system' started by jooleanlogic, Feb 10, 2019.

  1. jooleanlogic

    jooleanlogic

    Joined:
    Mar 1, 2018
    Posts:
    164
    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
    Guerro323 likes this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

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