How to Loop over Enities in IJobs

Im trying to learn ECS and Jobs. So far so good. Ive been able to get a simple Icosahedron build into ECS using ComponentSystem, JobComponentSystem and now trying IJobs. But im confused on how to access my entities without GetEntityQuery and Entities.With(_query).ForEach inside my Execute(). Please help and thank you in advance.

        /// <summary>
        /// Start this instance.
        /// </summary>
        protected void Start()
        {
            //
            // Gee ref to EntityManager
            _entityManager = World.Active.EntityManager;

            //
            // Define Archetype
            tileArchetype = _entityManager.CreateArchetype( typeof(VertexComponent),
                                                            typeof(VertexTrianglesComponent),
                                                            typeof(VertexSubdivisionComponent));

            //
            // Create Entities
            NativeArray<Entity> entityArray = new NativeArray<Entity>(12, Allocator.Temp);
            _entityManager.CreateEntity(tileArchetype, entityArray);

            //
            // Create and Schedule Job
            BuildIcosahedronJob job = new BuildIcosahedronJob()
            {
                icosahedronPosBuffer = new NativeArray<float3>(icosahedronPosData, Allocator.TempJob)
            };

            _jobHandle = job.Schedule();
        }

        /// <summary>
        /// Lates the update.
        /// </summary>
        public void LateUpdate()
        {
            _jobHandle.Complete();
        }

        /// <summary>
        /// Ons the destroy.
        /// </summary>
        private void OnDestroy()
        {
            icosahedronPosBuffer.Dispose();
        }


        #region JOBS -----------------------------------------------------------

        [BurstCompile]
        struct BuildIcosahedronJob : IJob
        {
            [DeallocateOnJobCompletion]
            public NativeArray<float3> icosahedronPosBuffer;

            public void Execute()
            {
                // TODO : Loop Over Entities !! But How ??

                //v.position = icosahedronPos[index];
                //v.index = index;
                //t.connectedTriA = -1;
                //t.connectedTriB = -1;
                //t.connectedTriC = -1;
                //t.connectedTriD = -1;
                //t.connectedTriE = -1;
                //t.connectedTriF = -1;
                //s.divisionLevel = 0;

            }
        }
        #endregion

Take a look at IJobForEach.

https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/Samples/Assets/HelloCube/2.%20IJobForEach/RotationSpeedSystem_IJobForEach.cs

Start by following the HelloCube series here:

Hi Joachim, thanks for the reply.

I worked through that whole repository and was able to write my simple code over using each of those system.

Here is question. To iterate through entities I need a system and implement onUpdate(). In my case it’s a simple subdivision algorithm that runs depending on the number of subdivision levels needed, creating and settings entities .

After I’m done with my subdivision the onUpdate method is not needed only the entities I created.

What is the correct way to handle this situation, should I dispose of the whole system after I’m done or not use a system at all and try create and set entity in a simple Job.

Still learning here so any help would be appropriate

Thanks

It’s all about data, no data == no work, which mean you can create some entity (like event) and your system should have entity query by this entity archetype and when you need your subdivision you’ll create this entity and system doing stuff, and after that just idle. It’s ideal way IMO. Also you can disable auto creation for this system and just call update manually when you need.

Thanks for the reply eizenhorn, are there any doc on auto create, this is the first time ive heard of it.

DisableAutoCreation is attribute which you can use on your systems to prevent their creation and putting in to player loop, instead of this you should create them manualy and put to player loop or call update by yourself.

1 Like