Loop through ComponentDataFromEntity

Is there a way to do that ?

[BurstCompile]
    struct Triggerjob : IJobForEachWithEntity<TriggerTag>
    {
        [ReadOnly] public ComponentDataFromEntity<Translation> Translations;
        [ReadOnly] public ComponentDataFromEntity<TriggerTag> TriggerTags;

        public void Execute(Entity entity, int index,[ReadOnly] ref TriggerTag trigger ,ref Translation translation)
        {
      
           for (int i = 0; i < TriggerTags.lenth; i++ )
          {
            if (translation.value == Translations[i].value)  {do something}
           }
    }

I think this should be
in TriggerTag trigger

As for looping through all elements of a ComponentDataFromEntity, I think that is the wrong way to do it, even if it compiles

1 Like

vestigial thank you !
actually i just want to calculate the distance between each entity and the other entities, so if one entity is close enough to the other entity some functions will be run on it
something look like OnTriggerEnter
But I don’t know what is the best way to do this XD
how can i loop throw other entities Translations ?

Ooh, in that case yeah I guess looping through all of them makes sense. Sorry. I’m sure there are all kinds of optimizations for that. The DOTS physics package might have some kind of OnTriggerEnter

Yes that’s right they have OnTriggerEnter Event , but using TriggerEnter Event
Requires (physics shapes) which is very expensive x.x

That’s for Entities.ForEach.

The way most people do this is to use EntityQuery.ToComponentDataArray/ToEntityArray for the components they want to read.
The more advanced way is to use manual chunk iteration.
The most advanced way is to use an intermediate acceleration structure so that your algorithm can run in O(n log n) rather than O(n^2). Though this strategy is only worth it if you have a few hundred entities or already have the algorithm written.

1 Like

DreamingImLatio The first way looks pretty cute :smile:
I will check it thank you !