Search Unity

How to Loop over Enities in IJobs

Discussion in 'Entity Component System' started by RoughSpaghetti3211, May 22, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    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.

    Code (CSharp):
    1.         /// <summary>
    2.         /// Start this instance.
    3.         /// </summary>
    4.         protected void Start()
    5.         {
    6.             //
    7.             // Gee ref to EntityManager
    8.             _entityManager = World.Active.EntityManager;
    9.  
    10.             //
    11.             // Define Archetype
    12.             tileArchetype = _entityManager.CreateArchetype( typeof(VertexComponent),
    13.                                                             typeof(VertexTrianglesComponent),
    14.                                                             typeof(VertexSubdivisionComponent));
    15.  
    16.             //
    17.             // Create Entities
    18.             NativeArray<Entity> entityArray = new NativeArray<Entity>(12, Allocator.Temp);
    19.             _entityManager.CreateEntity(tileArchetype, entityArray);
    20.  
    21.             //
    22.             // Create and Schedule Job
    23.             BuildIcosahedronJob job = new BuildIcosahedronJob()
    24.             {
    25.                 icosahedronPosBuffer = new NativeArray<float3>(icosahedronPosData, Allocator.TempJob)
    26.             };
    27.  
    28.             _jobHandle = job.Schedule();
    29.         }
    30.  
    31.         /// <summary>
    32.         /// Lates the update.
    33.         /// </summary>
    34.         public void LateUpdate()
    35.         {
    36.             _jobHandle.Complete();
    37.         }
    38.  
    39.         /// <summary>
    40.         /// Ons the destroy.
    41.         /// </summary>
    42.         private void OnDestroy()
    43.         {
    44.             icosahedronPosBuffer.Dispose();
    45.         }
    46.  
    47.  
    48.         #region JOBS -----------------------------------------------------------
    49.  
    50.         [BurstCompile]
    51.         struct BuildIcosahedronJob : IJob
    52.         {
    53.             [DeallocateOnJobCompletion]
    54.             public NativeArray<float3> icosahedronPosBuffer;
    55.  
    56.             public void Execute()
    57.             {
    58.                 // TODO : Loop Over Entities !! But How ??
    59.  
    60.                 //v.position = icosahedronPos[index];
    61.                 //v.index = index;
    62.                 //t.connectedTriA = -1;
    63.                 //t.connectedTriB = -1;
    64.                 //t.connectedTriC = -1;
    65.                 //t.connectedTriD = -1;
    66.                 //t.connectedTriE = -1;
    67.                 //t.connectedTriF = -1;
    68.                 //s.divisionLevel = 0;
    69.  
    70.             }
    71.         }
    72.         #endregion
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    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
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    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.
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Thanks for the reply eizenhorn, are there any doc on auto create, this is the first time ive heard of it.
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    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.
     
    RoughSpaghetti3211 likes this.