Entities.ForEach doesn't run

Hello, I’m having issues getting a basic DOTS lambda to run. My OnUpdate() is as follows:

protected override void OnUpdate()
    {
        Debug.Log("situationRequestHandler");
        var buffer = Buffer;
        var situationBuilder = SituationBuilder;

        // Not sure if entityInQueryIndex will work for jobchunk id.
        Entities
        .ForEach((Entity entity, ref BuildSituationRequest request) =>
        {
            var e = buffer.CreateEntity(situationBuilder);
            var s = new Situation();
            s.stageId = request.stageId;
            buffer.SetComponent(e, s);
            buffer.DestroyEntity(entity);
        })
        .WithBurst()
        .Run();
    }

Right now I’m manually building a BuildSituationRequest entity for testing purposes. In my Entity Debugger I can clearly see that there is an entity with a BuildSituationRequest component on it. Under the “Used by Systems” tab, it knows it’s used by the system with OnUpdate() method showed above. Additionally, the Debug.Log in the OnUpdate is ran, so I know this System is running. However, the Entities.ForEach doesn’t run. In the Entities.ForEach I create an entity and destroy one. However, the original entity still exists and the new entity is never created. What could I be doing wrong?

What is Buffer?

If that’s EntityCommandBuffer you’re not using it right

Yes, it’s an EntityCommandBuffer. Please let me know where I’ve gone wrong

You’re never playing it back as far as I can tell so it’s not applying any changes.

Either you need to create it from a command buffer system to defer playback

protected override void OnCreate() {

    this.bufferSystem = this.World.GetOrCreateSystem<BeginPresentationEntityCommandBufferSystem>();
}

protected override void OnUpdate()
{
    var buffer = this.bufferSystem.CreateCommandBuffer();
  
    Entities.ForEach(() => { })...
  
    this.bufferSystem.AddJobHandleForProducer(this.Dependency);
}

or you need to play it back yourself

protected override void OnUpdate()
{
    var buffer = new EntityCommandBuffer(Allocator.TempJob);
  
    Entities.ForEach(() => { })...
  
    buffer.Playback(this.EntityManager);
    buffer.Dispose();
}
1 Like

Thank you very much… not sure how I didn’t find this.