Search Unity

Execute Code in between Entities.Foreach

Discussion in 'Entity Component System' started by dCalle, May 13, 2020.

  1. dCalle

    dCalle

    Joined:
    Dec 16, 2013
    Posts:
    55
    Hey folks, I ran into an issue.
    I wanted to run some code inbetween jobs. In this case it would be just to convert a queue into a loopable array.

    How do I achieve this? And is there another approach?


    Code (CSharp):
    1.  
    2. [UpdateBefore(typeof(BuildPhysicsWorld))]
    3. public class GravitySystem : SystemBase
    4. {
    5.     protected override void OnUpdate()
    6.     {
    7.         var buffer = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>().CreateCommandBuffer()
    8.             .ToConcurrent();
    9.         var queue = new NativeQueue<GravityWellData>();
    10.         var queueWriter = queue.AsParallelWriter();
    11.         NativeArray<GravityWell> array;
    12.         var handle = Entities.ForEach((ref Entity entity, ref GravityWell well, ref Translation translation) =>
    13.         {
    14.             queueWriter.Enqueue(new GravityWellData
    15.             {
    16.                 entity = entity,
    17.                 translation = translation,
    18.                 velocity = EntityManager.HasComponent<PhysicsVelocity>(entity)
    19.                     ? EntityManager.GetComponentData<PhysicsVelocity>(entity)
    20.                     : default,
    21.                 well = well
    22.             });
    23.         }).ScheduleParallel(Dependency);
    24.  
    25.         // new job to convert queue into a loopable array
    26.  
    27.         Entities.ForEach((ref PrivateGravity gravity, ref PhysicsVelocity velocity) =>
    28.         {
    29.             if (gravity.targetGravityWell == Entity.Null)
    30.             {
    31.                 //loop array, assign closest well
    32.             }
    33.             //apply gravity
    34.         }).ScheduleParallel(handle);
    35.     }
    36. }
    37.  
    thanks
     
  2. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    dCalle likes this.
  3. dCalle

    dCalle

    Joined:
    Dec 16, 2013
    Posts:
    55
    nice^^, that sounds promising^^ thanks^^
     
    florianhanke likes this.