Get Entity in IJobChunk

Does anybody know how the entities within IJobChunk can be grabbed? I have a simple code where I browse through entities, spawn new ones at the position of the old ones and remove the old ones afterwards. The interesting part is in line 8 (entityrefs = ???), as we don’t talk about components, I cannot use ComponentTypeHandle…

 public struct SplitSoldierEntities : IJobChunk  {
        public ComponentTypeHandle<LocalTransform> localtransformtypehandle;
        public EntityCommandBuffer.ParallelWriter _ecb;

        public void Execute(in ArchetypeChunk chunk, int entityInQueryIndex, bool useEnabledMask, in v128 chunkEnabledMask)
        {
            NativeArray<LocalTransform> localtransforms = chunk.GetNativeArray<LocalTransform>(ref localtransformtypehandle);
            NativeArray<Entity> entityrefs = ???

            for (int i = 0; i < localtransforms.Length; i++)
            {
                LocalTransform localtr = localtransforms[i];
                //spawn new ones
                //...
                //destroy old ones
                _ecb.DestroyEntity(entityInQueryIndex, entityrefs[i]);
            }
        }
    }
public void SplitSoldiersInGroup(ref SystemState state, Entity group) {
    EntityManager entitymanager = World.DefaultGameObjectInjectionWorld.EntityManager;
    EntityQuery groupquery = groupdata.soldiersformovement;

    SplitSoldierEntities splitjob = new()
    {
        localtransformtypehandle = state.GetComponentTypeHandle<LocalTransform>(),
    };
    state.Dependency = splitjob.ScheduleParallel(groupquery, state.Dependency);
}

EntityTypeHandle

1 Like

Use EntityTypeHandle in pretty much the same way you’re using ComponentTypeHandle.

With that said, if your job doesn’t do anything special that needs to have the context of a full chunk (you mention “simple code” and the posted code doesn’t do anything necessitating an IJobChunk), you can use IJobEntity to make the job simpler and use an Entity parameter for the current entity.

If you are going to use IJobChunk, be cautious if your query involves any enableable components - in these cases, you would best make use of ChunkEntityEnumerator as seen in sample code for IJobChunk to properly filter which entities you want to work on within the chunk.

Also, you might want to rename the second parameter in your Execute method to unfilteredChunkIndex to avoid later confusion, to describe the actual meaning of the parameter (index of the chunk in the query without factoring in extra query filters that exclude some chunks from your basic component-based query).

Another tip, you can get EntityManager directly from the passed SystemState in your second snippet instead of going through World.DefaultGameObjectInjectionWorld, which might not refer to the world the system is executing in.

3 Likes

Thanks for your answers guys! First tries look promising!

@Spy-Master: IJobChunk is used to only iterate over a certain chunk (=group of soldiers, derived from an entityquery) out of many groups. This way the function is handled much faster than iterating over all entities…

IJobEntity itself also accepts a query. If your filtering just requires using an entity query, then the IJobEntity code would be a lot simpler. IJobEntity is sourcegenned on top of IJobChunk, the underlying chunk-based filtering is the same.