Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Getting the index of Entities.ForEach()

Discussion in 'Entity Component System' started by kmcphail, Sep 24, 2020.

  1. kmcphail

    kmcphail

    Joined:
    Jan 26, 2019
    Posts:
    8
    Code (CSharp):
    1. var tempBoidPositionArray =  m_BoidQuery.ToComponentDataArray<BoidPositionComponent>(Allocator.Temp);
    2.         NativeArray<float3> boidPositions = new NativeArray<float3>(boidCount, Allocator.Temp);
    3.        
    4.         Entities.With(m_BoidQuery)
    5.             .ForEach((
    6.             ref BoidVelocityComponent movementVelocityComp,
    7.             ref BoidPositionComponent boidPositionComp) => {
    8.                 boidPositions[entityIndex] = tempBoidPositionArray[entityIndex].position;
    9.             });
    Right now I do not know how to get the current entities index to make this happen
     
  2. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Use
    int entityInQueryIndex
    as described in https://docs.unity3d.com/Packages/c...ntities_foreach.html#special-named-parameters.
     
  3. kmcphail

    kmcphail

    Joined:
    Jan 26, 2019
    Posts:
    8
    Code (CSharp):
    1. Entities.With(m_BoidQuery)
    2.             .ForEach((
    3.             int entityInQueryIndex,
    4.             ref BoidVelocityComponent movementVelocityComp,
    5.             ref BoidPositionComponent boidPositionComp) => {
    6.                 boidPositions[entityInQueryIndex] = tempBoidPositionArray[entityInQueryIndex].position;
    7.             });
    The problem I get with using int entityInQueryIndex is that get the following error:

    EntityQueryBuilder.F_E does not take 3 arguments
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,937
    Use SystemBase, not ComponentSystem.
     
    florianhanke likes this.
  5. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    My bad, I assumed you were using SystemBase.
     
  6. kmcphail

    kmcphail

    Joined:
    Jan 26, 2019
    Posts:
    8
    No problem. All is well. Nice to have something simple fix my frustration
     
    florianhanke likes this.