Foreign Entities inside foreach / Access buffer from another entities (solved)

Hi, Here a bit of context , am trying to compare 2 paths of node(Int) between two entities and wanted to know how i could acces DynamicBuffer/Component of a foreign entities inside the foreach lamba or job.

A. Is there already a known way to access data from a foreign entities in foreach?
B. Do im bound to use EntityManager.GetBuffer and run the job on the maintread?
C. Do I need to further divide the work through multiple job/systems?

PS : i know that my code currently produce aliasing with the lookup. Will try to replace this with a entityquery.

(pastebin link : UnityForum#805 - Pastebin.com)

    protected override void OnUpdate()
    {

        EntityCommandBuffer.Concurrent ecb = endSimulationEcbSystem.CreateCommandBuffer().ToConcurrent();
        CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;

        BufferFromEntity<NodeIDPath> bufferLookup = GetBufferFromEntity<NodeIDPath>();
        ComponentDataFromEntity<UnitsGroup> componentLookup = GetComponentDataFromEntity<UnitsGroup>();

        float _castRadius = castRadius;

        //Find already existing unitsGroup in proximity of the unit
        JobHandle FindUnitsGroupInProximityJobHandle = Entities
        .ForEach((Entity entity, int entityInQueryIndex
        , ref NeedUnitsGroup needUnitsGroup
        , in OwnerComponent ownerComponent
        , in Translation translation
        , in DynamicBuffer<NodeIDPath> nodePath) =>
        {
            NativeList<int> hitsIndices = new NativeList<int>(Allocator.Temp);

            CollisionFilter filter = new CollisionFilter
            {
                BelongsTo = 3,
                CollidesWith = 3,
                GroupIndex = 0
            };
            //Check for nearby unitsgroup already existing
            Aabb aabb = new Aabb
            {
                Min = translation.Value + new float3(-_castRadius, 0, -_castRadius),
                Max = translation.Value + new float3(_castRadius, 0, _castRadius)
            };
            OverlapAabbInput overlapAabbInput = new OverlapAabbInput
            {
                Aabb = aabb,
                Filter = filter,
            };
            if (collisionWorld.OverlapAabb(overlapAabbInput, ref hitsIndices))
            {
                //Foreach detected unitsGroup check we compare the unitsGroup node vs the one of the units
                for (int i = 0; i < hitsIndices.Length; i++)
                {
                    Entity unitsGroupEntity = collisionWorld.Bodies[hitsIndices[i]].Entity;
                    //check if of the same owner
                    if (componentLookup[unitsGroupEntity].owner != ownerComponent.owner)
                    {
                        continue;
                    }
                    //Compare the nodePath with the one units
                    DynamicBuffer<NodeIDPath> unitsGroupNodesPath = bufferLookup[unitsGroupEntity];
                    //Check if the nodepath are equal if yes we can stop here , we found a corresponding group
                    if (unitsGroupNodesPath.Equals(nodePath))
                    {
                        needUnitsGroup.foundUnitsGroupEntity = unitsGroupEntity;
                        break;
                    }
                    //Check if the second node is different, if yes , this unitsgroup is not valid for the current unit
                    if (unitsGroupNodesPath[1] != nodePath[1] || unitsGroupNodesPath.Length <= 2 ||        nodePath.Length <= 2)
                    {
                        continue;
                    }
                    //Finally we will check if the current unit can follow the group for part of the path and separate later
                    //Take the shorter one to loop trough both and see if they have part of path in common
                    int loopLength = unitsGroupNodesPath.Length;
                    if (nodePath.Length < unitsGroupNodesPath.Length)
                    {
                        loopLength = nodePath.Length;
                    }
                    for (int x = 2; x < loopLength; x++)
                    {
                        if (nodePath[x] != unitsGroupNodesPath[x])
                        {
                            needUnitsGroup.foundUnitsGroupEntity = unitsGroupEntity;
                            ecb.AddComponent<UnitsGroupSeparation>(entityInQueryIndex, entity, new UnitsGroupSeparation(x - 1));
                            break;
                        }
                    }
                }
            }
            hitsIndices.Dispose();

            needUnitsGroup.proximityCheck = true;
        }).Schedule(Dependency);

You already have it with ComponentDataFromEntity and BufferFromEntity. Unless your issue is writing to these types inside the lambda. In that case you need to rethink your problem.

No , i only need to read the data, but how do i avoid aliasing error in this case?

Remove the nodePath argument in the lambda and use the lambda’s entity field to look up the buffer.

Oh i see … thanks for the help!

Just one last thing if you may , in this case it’s better to use lookup like i did or i should use entityQuery?

It is not obvious from your code in what way you would use an EntityQuery instead. Could you elaborate?

Sry for the lack of clarity, Im only checking for equality and loop over the shorter path (DynamicBuffer) to see if the two path have part of them in common. I was only asking in term of performance if there’s a difference between doing so with “lookup” or “EntityQuery”.

Thanks in advance.

In what way would you use an EntityQuery instead of BufferFromEntity? What API would you use? EntityQuery is tied to a few different mechanisms with drastically different behavior and implications.