GetBufferFromEntity error inside a job

I was looking for a way to get buffer data from inside a job and test how it works and if it is burstable

but I’ve already hit a wall as soon as i’ve started lol

https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/dynamic_buffers.html

var lookup = GetBufferFromEntity<EcsIntElement>();
    var buffer = lookup[myEntity];
    buffer.Append(17);
    buffer.RemoveAt(0);

seems like it’s outdated or something is missing there, I keep getting this error

Assets\Scripts\TEST_Burst_DynamicBuffer\Sistemas\Sys_BurstDynamic_Test.cs(15,26): error CS0120: An object reference is required for the non-static field, method, or property 'JobComponentSystem.GetBufferFromEntity<Buff_TestChange>(bool)'

here is my system and the buffer in question

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;

[UpdateInGroup(typeof(Barrier_BurstDynamic))]
public class Sys_BurstDynamic_Test : JobComponentSystem
{
    private struct Job : IJobForEachWithEntity<Tag_BusterDynamic_Test>
    {
        public void Execute (Entity ent, int index, ref Tag_BusterDynamic_Test tag)
        {
            var lookup = GetBufferFromEntity<Buff_TestChange>();
            var buffer = lookup[ent];

        }
    }

    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        Job job = new Job();
        return job.Schedule(this, inputDeps);
    }

}

and the buffer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;

[InternalBufferCapacity(10)]
public struct Buff_TestChange : IBufferElementData
{
    public int id;
}

simple copy paste, I have no idea what i’m doing wrong O.o

This is doable, but instead pass the lookup structure (Should be BufferFromEntity<Buff_TestChange>) to the Job, with a [DisableNativeParallelForRestriction] since there will only be one access per entity with a buffer.

1 Like

seems like that will work out, I just need the directive to use the [DisableNativeParallelForRestriction] xD
already tried Unity.Jobs.Lowlevel but it says the directive cant be found

[edit]
btw, if I use readonly can it be used in parallel?

It’s [NativeDisableParallelForRestriction] in Unity.Collections

I think also you should have [RequireComponentTag(typeof(Buff_TestChange))] on the job to ensure the entity’s you’re iterating actually have that component.

It’s my understanding that it’ll be parallel for both read/write within this job but if you want parallel with other jobs reading the same component then it’ll need to be readonly.

1 Like

perfect, got it working now, thanks for the help guys