Search Unity

A bit confused about the entity command buffer best practices

Discussion in 'Data Oriented Technology Stack' started by sebas77, Aug 20, 2019.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    866
    so far I understood that every ComponentSystem can create its own ECB, but I am not sure if this is a good practice as I can see that in some examples the ECB from special systems are used instead. these are for example BeginSimulationEntityCommandBufferSystem and
    EndSimulationEntityCommandBufferSystem. My guess is that local ECBs are flushed too often.
    I understand that ECBs are nothing more than queues, but I assumed that the framework was taking care of them, instead it seems that it can be up to the user to flush them when necessary. The flush happens on the mainthread (as far as I understood).
    are these special systems just for convenience? Can I use a custom ECB and flush it when I want? Is it a good idea to inject the ECB into other systems and use in them? Currently I am thinking to have a custom Buffer System that my system can get through the world and get the ECB from it.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,667
    Every ComponentSystems have their own ECB which is simply executed after update.

    However JobComponentSystems do not have an ECB as they should be sync point free. Therefore they rely on another sync point at a later time, commonly the end of frame (EndSimulationEntityCommandBufferSystem), to process their ECB changes.

    You are welcome to create your own ECB (or implement your own EntityCommandBufferSystem). There's no direct harm in it and its quite valid in a ComponentSystem, however there is no real point in sharing a ECB between multiple ComponentSystems expect if you need to delay changes for some reason.
     
    sebas77 likes this.
  3. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    866
    so in case of jobs normally I should fetch the ECB from the EndSimulationEntityCommandBufferSystem?
     
  4. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    You basically store a reference to the BufferSystem you want, and when sceduling the job BufferSystem.CreateCommandBuffer()

    Note the parallel variant if needed. You then need to add the job as a dependency to the BufferSystem so you dont run into scheduling issues