I need a stable index into an array.
I’ve have found something potentially useful in the Boids Sample code…
var copyTargetPositionsJobHandle = Entities
.WithName("CopyTargetPositionsJob")
.WithAll<BoidTarget>()
.WithStoreEntityQueryInField(ref m_TargetQuery)
.ForEach((int entityInQueryIndex, in LocalToWorld localToWorld) =>
{
copyTargetPositions[entityInQueryIndex] = localToWorld.Position;
})
.ScheduleParallel(Dependency);
What is entityInQueryIndex?
It is the same index as the index IJobForEachWithEntity gave you. It is also the same index that the entity’s data would end up in if you did an EntityQuery.ToComponentDataArray(). This index is only stable for as long as there are no structural changes, so you don’t really want to rely on it outside the scope of a system.
int entityInQueryIndex — the index of the entity in the list of all entities selected by the query. Use the entity index value when you have a native array that you need to fill with a unique value for each entity. You can use the entityInQueryIndex as the index in that array. The entityInQueryIndex should also be used as the jobIndex for adding commands to a concurrent EntityCommandBuffer.
documentation very good for this