#ReadOnly Error in Job

1 messages · Page 1 of 1 (latest)

lament ivy
#

I have the following job that finds chunks with high priority:

[BurstCompile]
public struct PrioritizeChunksJob : IJob
{
    [NativeDisableUnsafePtrRestriction] public EntityQuery query;

    public NativeArray<Entity> chunks;
    public TerrainGrid grid;
    public int capacity;

    public NativeArray<int> chunkCount;
    private int count;

    public void Execute()
    {
        query.AddSharedComponentFilter(new ChunkLod { value = 0 });

        for (int lod = 0; lod < grid.lodCount && capacity > 0; lod++)
        {
            AddHighestPriorityChunks(lod);
        }

        chunkCount[0] = count;
    }

    private void AddHighestPriorityChunks(int lod)
    {
        query.SetSharedComponentFilter(new ChunkLod { value = lod });

        NativeArray<Entity> entities = query.ToEntityArray(Allocator.Temp);

        for (int i = 0; i < entities.Length && capacity > 0; i++, capacity--)
        {
            chunks[count] = entities[i];
            count++;
        }

        entities.Dispose();
    }
}
#

And I'm getting the following error: System::InvalidOperationException: The Unity.Entities.EntityTypeHandle has been declared as [WriteOnly] in the job, but you are reading from it.
for the following line: ``` NativeArray<Entity> entities = query.ToEntityArray(Allocator.Temp);

Any ideas why? I never declared the query as WriteOnly
fierce hemlock
#

Don't think you can use query in jobs at all

lament ivy
#

ah yikes. Should I turn this into a common function then?

fierce hemlock
#

yeah

lament ivy
#

Is it still possible to run this asynchronously?

fierce hemlock
#

you can make all query operations beforehand and then pass to job

lament ivy
#

👍 thanks!