Execute Code in between Entities.Foreach

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?

[UpdateBefore(typeof(BuildPhysicsWorld))]
public class GravitySystem : SystemBase
{
    protected override void OnUpdate()
    {
        var buffer = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>().CreateCommandBuffer()
            .ToConcurrent();
        var queue = new NativeQueue<GravityWellData>();
        var queueWriter = queue.AsParallelWriter();
        NativeArray<GravityWell> array;
        var handle = Entities.ForEach((ref Entity entity, ref GravityWell well, ref Translation translation) =>
        {
            queueWriter.Enqueue(new GravityWellData
            {
                entity = entity,
                translation = translation,
                velocity = EntityManager.HasComponent<PhysicsVelocity>(entity)
                    ? EntityManager.GetComponentData<PhysicsVelocity>(entity)
                    : default,
                well = well
            });
        }).ScheduleParallel(Dependency);

        // new job to convert queue into a loopable array

        Entities.ForEach((ref PrivateGravity gravity, ref PhysicsVelocity velocity) =>
        {
            if (gravity.targetGravityWell == Entity.Null)
            {
                //loop array, assign closest well
            }
            //apply gravity
        }).ScheduleParallel(handle);
    }
}

thanks

You could use Job.WithCode if I understand you correctly: Using Job.WithCode | Entities | 0.8.0-preview.8

1 Like

nice^^, that sounds promising^^ thanks^^

1 Like