Search Unity

Entity Index ForEach.Entities?

Discussion in 'Entity Component System' started by Mr-Mechanical, Apr 5, 2020.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I need a stable index into an array.
     
  2. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I've have found something potentially useful in the Boids Sample code...

    Code (CSharp):
    1. var copyTargetPositionsJobHandle = Entities
    2. .WithName("CopyTargetPositionsJob")
    3. .WithAll<BoidTarget>()
    4. .WithStoreEntityQueryInField(ref m_TargetQuery)
    5. .ForEach((int entityInQueryIndex, in LocalToWorld localToWorld) =>
    6. {
    7.       copyTargetPositions[entityInQueryIndex] = localToWorld.Position;
    8. })
    9. .ScheduleParallel(Dependency);
    What is entityInQueryIndex?
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    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.
     
    Mr-Mechanical likes this.
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    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.

    https://docs.unity3d.com/Packages/c...ntities_foreach.html#special-named-parameters

    documentation very good for this
     
    Mr-Mechanical likes this.