Proper way of getting ECB in SystemBase

Hello all, in ISystem systems I am able to obtain Entity Command Buffer by typing:

 var ecb = SystemAPI.GetSingleton<BeginInitializationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);

SystemBase systems do not have SystemState state parameter on it’s methods and I was able to obtain ECB by these codes:

1) var ecb = World.GetOrCreateSystemManaged<BeginSimulationEntityCommandBufferSystem>().CreateCommandBuffer();
2) var ecb = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(World.Unmanaged);

Are both of these options valid in SystemBase, or is some of them preferred? Or should I choose something entirely different?

Thanks in advance for your answers!

Both are valid, but the first one has the caveat that that you need to call AddJobHandleForProducer if you use it in a job. The second option, using the singleton, sets up job dependencies automatically, and I would say that’s the better option of the two and also the option you should go with overall.

Also, you can use CheckedStateRef to get access to the system’s backing SystemState. Both ways of getting the WorldUnmanaged are valid.

2 Likes