Destroying Entities with EntityQueryDesc

Hello,

Wondering if I can get some eyes on this, Im not sure why this is not working or if there is a better way of doing this.

In my GameObjectConversionSystem im creating some entities.

The conversions in not a game object itself but using some data on a game object. Where Im having trouble is multiple entities are generated so I need to destroy all entities with VertexComponent and TriangleComponent before I recreate and cache them.

In the code below I attempt to destroy using EntityQueryDesc but my EntityQueryDesc is alway length 0. In the entity debugger I can see entities with VertexComponent & TriangleComponent

    public class HexSphereBuildConversionSystem : GameObjectConversionSystem
    {
        protected override void OnUpdate()
        {
            //
            // Find all Entities with component typeVertexComponent & TriangleComponent and destroy them
            var query = new EntityQueryDesc
            {
                Any = new ComponentType[] { typeof(VertexComponent), typeof(TriangleComponent) },
                None = Array.Empty<ComponentType>(), 
                All = Array.Empty<ComponentType>(), 
            };

            var group = this.DstEntityManager.CreateEntityQuery(query);
            Debug.Log(group.CalculateLength()); //  Is 0 Not sure why ??????????

            Entities.With(group).ForEach((Entity entity) =>
            {
                this.DstEntityManager.DestroyEntity(entity);
            });

            Debug.Log(group.CalculateLength()); // Is 0


            //
            // Create new entities using some field on this game object
            GameObject go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
            HexSphereBuildDataProxy data = go.GetComponent<HexSphereBuildDataProxy>();
            WORLD_SIZE worldSize = (Lightbringer.ECS.HexSphere.WORLD_SIZE)data.worldSize;
            int subdivisionsCount = data.subdivisionsCount;
            int raduis = data.raduis;
            int vertexCount = data.vertexCount;
            int triangleCount = data.triangleCount;

            //
            // Create  data
            HexSphereBuildData.verticies = new NativeArray<VertexComponent>(vertexCount, Allocator.Persistent);
            HexSphereBuildData.triangles = new NativeArray<TriangleComponent>(triangleCount, Allocator.Persistent);

            //
            // Build data
            HexSphereBuildUtils.BuildHexSphereSystem(subdivisionsCount, raduis, ref HexSphereBuildData.verticies, ref HexSphereBuildData.triangles);

            //
            // Create EntityManager
            EntityManager em = World.Active.EntityManager;

            //
            // Create Entity Archetype instantiation
            EntityArchetype archVertex = em.CreateArchetype(typeof(VertexComponent));
            EntityArchetype archTriangle = em.CreateArchetype(typeof(TriangleComponent));

            //
            // Create entities
            NativeArray<Entity> eVertces = new NativeArray<Entity>(vertexCount, Allocator.TempJob);
            NativeArray<Entity> enTriangles = new NativeArray<Entity>(triangleCount, Allocator.TempJob);
            em.CreateEntity(archVertex, eVertces);
            em.CreateEntity(archTriangle, enTriangles);
            Debug.Log(eVertces.Length + " " + enTriangles.Length);

            //
            // Create Entity EntityQuery
            EntityQuery eqVertexComp = em.CreateEntityQuery(ComponentType.ReadOnly<VertexComponent>());
            EntityQuery eqTriangleComp = em.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());

            //
            // Batcch Set entity component data
            eqVertexComp.CopyFromComponentDataArray<VertexComponent>(HexSphereBuildData.verticies);
            eqTriangleComp.CopyFromComponentDataArray<TriangleComponent>(HexSphereBuildData.triangles);
            Debug.Log(eVertces.Length + " " + enTriangles.Length);

            eVertces.Dispose();
            enTriangles.Dispose();
            HexSphereBuildData.verticies.Dispose();
            HexSphereBuildData.triangles.Dispose();
        }
    }
}

Any help would be appreciated

thanks

At a very quick glance I suspect you are getting your worlds mixed up.

EntityManager em = World.Active.EntityManager;

-edit-

Should elaborate. GameObject conversion happens in it’s own separate world.

var gameObjectWorld = new World("GameObject World");
gameObjectWorld.CreateSystem<GameObjectConversionMappingSystem>(dstEntityWorld, sceneGUID, conversionFlags);

While you are creating your entities in the active world but querying the conversion world.

OK I did not know its creates in separate world. Thanks for the help.

How do I track and access the different worlds ? I also see an editor world, is that a thing too

EDIT : OK i see what you mean , thanks for the help

var group = this.DstEntityManager.CreateEntityQuery(query);
Entities.With(group).ForEach((Entity entity) =>
{
    this.DstEntityManager.DestroyEntity(entity);
});

You can just use:

this.DstEntityManager.DestroyEntity(this.DstEntityManager.CreateEntityQuery(query));

Should be faster :slight_smile:

1 Like

Awesome thank you.

@tertle yup you were correct I was in wrong world.