Boolean in (new) GetBuffer<T>(Entity, Boolean) in a system has no effect

I’m trying to get a read only buffer from a singleton in a job with GetBuffer(Entity, true), but I’m still getting:

“”
Method GetBuffer is giving write access to component data BlobUnitAbilitiesRef in an Entities.ForEach. The job system cannot guarantee the safety of that invocation. Either change the scheduling from ScheduleParallel to Schedule or access through a captured ComponentDataFromEntity and mark it with WithNativeDisableParallelForRestriction if you are certain that this is safe.
“”

Looks like a bug.

Have you marked it on the actual job as readonly?
[ReadOnly] or
WithReadOnly(buffer) for entities.ForEach

No, because I wanted to get a buffer from an entity reference from a component while in the foreach method. I don’t have a reference for the buffer before the Entities.ForEach.

If I use GetBufferFromEntity then I can mark it as WithReadOnly as you mention.

The GetBuffer(entity, readOnly) seems to me to be targeting that use case, as the GetComponent(enttiy) does, but probably that assumption is wrong. As they are quite similar it’d be useful if the documentation declared the distinction.

readOnly in GetBuffer is more of a guideline as I understand, and can return a writeable buffer in some cases. I’ve done this with DynamicBuffers previously and it seems to work. Not sure if it’s the same with other buffer types, or if this is a good way to do it. If there’s something better out there I’d like to know as well.

DynamicBuffer<FooBuffer> fooBuffer;
bool gotBuffer = GetBufferFromEntity<FooBuffer>(true).TryGetBuffer(entityWithBuffer, out fooBuffer)

Yes. I think this is a bug. There are two types of ReadOnly. The first is what tells the automatic dependency management of ECS to fetch the correct JobHandles. The second tells the job system about the safety of the data. The GetBuffer shorthand is only specifying the former, and not the latter, which means it is not correctly autogenerating the [ReadOnly] in the codegenned IJobEntityBatch. The workaround is to use BufferFromEntity explicitly along with .WithReadOnly.

2 Likes

Seems to still be the case for entities 1.0.0-exp-8. They did mention (a long time ago though) the codegen for this would be rather hard to implement.

But it’d be nice if it gets fixed, as it’ll reduce a lot of boilerplates for some of my usages.