Iterating Through Entities In Ijobchunk

How does one get the Entities in the current Chunk in a IJobChunk?
GetNativeArray seems to be aimed at getting componentdata, not Entities…

(Trying to copy Entities to a list if one of their components matches a certain value)

Use GetNativeArray with ArchetypeChunkEntityType

1 Like

Here is an example:

protected override JobHandle OnUpdate(JobHandle inputDeps)
{
    var entityType = GetArchetypeChunkEntityType();
    var coordinatesType = GetArchetypeChunkComponentType<Coordinates>();
    
   JobChunkExtensions.Schedule(new AddNewUnitsToCacheJob
   { 
    EntityType = entityType,
    CoordinatesType = coordinatesType
   }, _uncachedUnits);
}

private struct AddNewUnitsToCacheJob : IJobChunk
 {
            [ReadOnly] public ArchetypeChunkEntityType EntityType;
            [ReadOnly] public ArchetypeChunkComponentType<Coordinates> CoordinatesType;

            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                var entities = chunk.GetNativeArray(EntityType);
                var coordinates = chunk.GetNativeArray(CoordinatesType);
                for (var i = 0; i < chunk.Count; i++)
                {
         // do what you need to do
                }
            }
}