Hi all, one thing I still find a bit confusing from all the learning resources is how we are supposed to work with dynamic buffers in jobs. I have the following questions to better understand the topic:
1) ReadOnly dynamic buffers
Documentation basically says we should always use BufferLookup for accessing dynamic buffers from jobs, but it is also possible to pass buffer directly to the job like this:
// in system:
new BufferExampleJob
{
PrefabCollection = SystemAPI.GetSingletonBuffer<PrefabCollection>()
}
.Schedule(state.Dependency);
// job:
public partial struct BufferExampleJob : IJobEntity
{
[ReadOnly] public DynamicBuffer<PrefabCollection> PrefabCollection;
public void Execute(....)
{
// loop through buffer, instantiate the prefabs or whatever
}
}
Is this considered as a bad practice for some reason? For example I have a lot of read only singleton buffers in my project and accessing buffer directly instead of using lookups contributes to code clarity.
2) Converting to NativeArray
I’ve also seen practice of converting DynamicBuffer to NativeArray before it is passed to the job. Is this in general recommended? What are advantages of doing this?
3) Writing to dynamic buffers in jobs
Very similar to question nr. 1 - why should I always use BufferLookup when (in some cases) I can pass buffer directly to the job and work directly with it?
Also, when I want to add element to the buffer I can also use EntityCommandBuffer AppendToBuffer method - I guess ECB usage is preferred instead of direct usage of DynamicBuffer.Add()?
Thanks in advance for your answers! I’m looking to gain a better understanding of the whole dynamic buffer topic!