ForEach single Entity case

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.

// This is all I need
ForEach( (Entity entity) => {
    ...
}, GetComponentGroup(typeof(MySystemStateTagComponent)) );

// But I have to do this
ForEach( (Entity entity, ref Position pointlessComponent) => {
    ...
}, 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.

protected delegate void F_E(Entity entity);

unsafe protected void ForEach(F_E operate, ComponentGroup group)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
    EntityManager.m_InsideForEach++;
    try
#endif
    {
        var entityType = GetArchetypeChunkEntityType();

        using (var chunks = group.CreateArchetypeChunkArray(Allocator.TempJob))
        {
            foreach (var chunk in chunks)
            {
                var length = chunk.Count;

                var entityArray = chunk.GetNativeArray(entityType);
                for (int i = 0; i < length; ++i)
                    operate(entityArray[i]);
            }
        }
    }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
    finally
    {
        EntityManager.m_InsideForEach--;
    }
#endif
}
2 Likes

It will be in the next release.

3 Likes