How to do n^2 comparisons of entities of a specific archetype?

For example this would be how this is done in C#:

for(int i = 0; i < fishes.length; i++) {
      for(int j = i+1; j < fishes.length; j++) {
             CompareFish(fishes[i], fishes[j]);
      }
}

Thanks.

First, copy the components to a NativeArray using EntityQuery.ToComponentDataArray or a custom job. Then you can run a job on that algorithm or if you want access to one of the two entities in chunk memory you can use the entityInQueryIndex if you are using a lambda or the int argument in an IJobForEachWithEntity to represent “i” in the snippet you posted. The component passed in would represent fishes*.*
If your intent is to do chunk iteration and write to both entities in a parallel job, that’s a race condition. You need a special kind of islanding acceleration structure to do that safely.

1 Like