Selecting a ComponentGroup from multiple EntityArchetypeQueries

Hi all

Currently I have a system which uses these two component groups.

squadGroup = GetComponentGroup(
    ComponentType.ReadOnly(typeof(Navigate)),
    ComponentType.ReadOnly(typeof(Squad)),
    ComponentType.ReadOnly(typeof(Waypoint))
);
squaddieGroup = GetComponentGroup(
    ComponentType.ReadOnly(typeof(Squaddie)),
    ComponentType.ReadOnly(typeof(Position)),
    typeof(Waypoint)
);

However, now I noticed that the system runs when either group has matching entities. I need it to only run when both groups match. So I’m using EntityArchetypeQueries, as follows:

            squadQuery = new EntityArchetypeQuery
            {
                Any = Array.Empty<ComponentType>(),
                None = Array.Empty<ComponentType>(),
                All = new[]
                {
                    ComponentType.ReadOnly(typeof(Navigate)),
                    ComponentType.ReadOnly(typeof(Squad)),
                    ComponentType.ReadOnly(typeof(Waypoint))
                }
            };
            squaddieQuery = new EntityArchetypeQuery
            {
                Any = Array.Empty<ComponentType>(),
                None = Array.Empty<ComponentType>(),
                All = new[]
                {
                    ComponentType.ReadOnly(typeof(Squaddie)),
                    ComponentType.ReadOnly(typeof(Position)),
                    typeof(Waypoint)
                }
            };
            combined = GetComponentGroup(squadQuery, squaddieQuery);

But now I am wondering how I can select the results of either query. Is it possible to only get the matching entities from one EntityArchetypeQuery?

If not, is there a way to do so? Thanks! :slight_smile:

It’s possible. Should be something like this (not compiling code, but you’ll get the idea I hope):

protected override void OnUpdate()
        {
            var squadChunks = entityManager.CreateArchetypeChunkArray(squadQuery, Allocator.TempJob);
            var navigateArchetype = GetArchetypeChunkComponentType<Navigate>();
            var entitiesArchetype = GetArchetypeChunkEntityType();

            for (int i = 0; i < squadChunks.Length; i++)
            {
                var chunk = squadChunks[i];
                var navigateDatas = chunk.GetNativeArray(navigateArchetype);
                var entities = chunk.GetNativeArray(entitiesArchetype); // you'll get only entities matching your squadQuery

                for (int j = 0; j < navigateDatas.Length; j++)
                {
                    var navigate = navigateDatas[j];
                    var entity = entities[j];
                    //...
                }
           }

            var squaddieChunks = entityManager.CreateArchetypeChunkArray(squaddieQuery, Allocator.TempJob);
            var squaddieArchetype = GetArchetypeChunkComponentType<Squaddie>();
            for (int i = 0; i < squadChunks.Length; i++)
               {
                   var chunk = squadChunks[i];
                   var squaddieDatas = chunk.GetNativeArray(squaddieArchetype);
                   var entities = chunk.GetNativeArray(entitiesArchetype); // you'll get only entities matching your squaddieQuery
                   // ...
1 Like