Search Unity

Entities.ForEach doesn't run

Discussion in 'Entity Component System' started by Blargenflargle, Aug 2, 2020.

  1. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    Hello, I'm having issues getting a basic DOTS lambda to run. My OnUpdate() is as follows:


    Code (CSharp):
    1.    
    2. protected override void OnUpdate()
    3.     {
    4.         Debug.Log("situationRequestHandler");
    5.         var buffer = Buffer;
    6.         var situationBuilder = SituationBuilder;
    7.  
    8.         // Not sure if entityInQueryIndex will work for jobchunk id.
    9.         Entities
    10.         .ForEach((Entity entity, ref BuildSituationRequest request) =>
    11.         {
    12.             var e = buffer.CreateEntity(situationBuilder);
    13.             var s = new Situation();
    14.             s.stageId = request.stageId;
    15.             buffer.SetComponent(e, s);
    16.             buffer.DestroyEntity(entity);
    17.         })
    18.         .WithBurst()
    19.         .Run();
    20.     }
    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?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    What is Buffer?

    If that's EntityCommandBuffer you're not using it right
     
  3. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    Yes, it's an EntityCommandBuffer. Please let me know where I've gone wrong
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    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

    Code (CSharp):
    1. protected override void OnCreate() {
    2.  
    3.     this.bufferSystem = this.World.GetOrCreateSystem<BeginPresentationEntityCommandBufferSystem>();
    4. }
    5.  
    6. protected override void OnUpdate()
    7. {
    8.     var buffer = this.bufferSystem.CreateCommandBuffer();
    9.  
    10.     Entities.ForEach(() => { })...
    11.  
    12.     this.bufferSystem.AddJobHandleForProducer(this.Dependency);
    13. }
    or you need to play it back yourself

    Code (CSharp):
    1. protected override void OnUpdate()
    2. {
    3.     var buffer = new EntityCommandBuffer(Allocator.TempJob);
    4.  
    5.     Entities.ForEach(() => { })...
    6.  
    7.     buffer.Playback(this.EntityManager);
    8.     buffer.Dispose();
    9. }
     
    florianhanke likes this.
  5. Blargenflargle

    Blargenflargle

    Joined:
    Feb 24, 2019
    Posts:
    92
    Thank you very much... not sure how I didn't find this.