Access dynamic buffers from jobs
If a job needs to lookup one or more buffers in its code, the job needs to use a BufferLookup
lookup table. You create these in systems and then pass them to jobs that need them.
Modify the job
In the job that requires random access to a dynamic buffer:
- Add a
ReadOnly
BufferLookup
member variable. - In the
IJobEntity.Execute
method, index theBufferLookup
lookup table by an entity. This provides access to the dynamic buffer attached to the entity.
public partial struct AccessDynamicBufferJob : IJobEntity
{
[ReadOnly] public BufferLookup<ExampleBufferComponent> BufferLookup;
public void Execute()
{
// ...
}
}
Modify the systems
In systems that create instances of the job:
- Add a
BufferLookup
member variable. - In
OnCreate
, useSystemState.GetBufferLookup
to assign theBufferLookup
variable. - At the beginning of
OnUpdate
, callUpdate
on theBufferLookup
variable. This updates the lookup table. - When you create an instance of the job, pass the lookup table to the job.
public partial struct AccessDynamicBufferFromJobSystem : ISystem
{
private BufferLookup<ExampleBufferComponent> _bufferLookup;
public void OnCreate(ref SystemState state)
{
_bufferLookup = state.GetBufferLookup<ExampleBufferComponent>(true);
}
public void OnUpdate(ref SystemState state)
{
_bufferLookup.Update(ref state);
var exampleBufferAccessJob = new AccessDynamicBufferJob { BufferLookup = _bufferLookup };
exampleBufferAccessJob.ScheduleParallel();
}
}