Must be a better way of doing this

Basically getting some values from component eg subdivisionCount to use in the next job for a NativeArray size. This seems wrong to me.

        [BurstCompile]
        struct GetPlanetSizeData : IJobForEach<PlanetSizeComponents>
        {
            [WriteOnly] public NativeArray<int>subdivisionCount;
            [WriteOnly] public NativeArray<float>raduis;
            [WriteOnly] public NativeArray<int>vertexCount;
            [WriteOnly] public NativeArray<int>triangleCount;


            public void Execute(ref PlanetSizeComponents ps)
            {
                subdivisionCount[0] = ps.SubdivisionsCount;
                raduis[0] = ps.Raduis;
                vertexCount[0] = ps.VertexCount;
                triangleCount[0] = ps.TriangleCount;

            }
        }

        #endregion


        #region MAIN THREAD ------------------------------------------------------

        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {


            var subdivisions = new NativeArray<int>(1, Allocator.TempJob);
            var raduis = new NativeArray<float>(1, Allocator.TempJob);
            var vertices = new NativeArray<int>(1, Allocator.TempJob);
            var triangles = new NativeArray<int>(1, Allocator.TempJob);

            inputDeps = new GetPlanetSizeData()
            {
                subdivisionCount = subdivisions,
                raduis = raduis,
                vertexCount = vertices,
                triangleCount= triangles

            }.ScheduleSingle(this, inputDeps);
      
           // NEXT JOB NEEDS Uses NativeArray with size of vertices

            inputDeps.Complete();

            subdivisions.Dispose();
            raduis.Dispose();
            vertices.Dispose();
            triangles.Dispose();

            return inputDeps;
        }

Why not just write PlanetSizeComponent into NativeArray instead of writing fields data separately?

If next job only need to read results of previous job it’s a good way. I use NativeQueue for those tasks. If next job require RW random access, you can use ComponentDataFromEntity and just collect all Entities in first job.

IJobParallelForDefer

https://docs.unity3d.com/Packages/com.unity.jobs@0.1/api/Unity.Jobs.IJobParallelForDefer.html

Thanks for the replies, I tried the more familiar option first which was the NativeArray suggestion. Seems to work ok but got a it messy.

Im going to try IJobParallelForDefer, didn’t even know this existed so would be interesting to try implement.

Anyone have a simple example on how to use IJobParallelForDefer

public class DeferredSystem : JobComponentSystem
{
    private struct FillJob : IJob
    {
        public int Count;
        public NativeList<int> ListToFill;

        public void Execute()
        {
            for (int i = 0; i < Count; i++)
            {
                ListToFill.Add(i);
            }
        }
    }

    private struct DeferredJob : IJobParallelForDefer
    {
       [ReadOnly] public NativeArray<int> DeferredList; //Read only just for example

        public void Execute(int index)
        {
            var value = DeferredList[index];
        }
    }
 
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var deferredList = new NativeList<int>(Allocator.TempJob);
    
        inputDeps = new FillJob()
        {
            Count      = Random.Range(1, 99),
            ListToFill = deferredList
        }.Schedule(inputDeps);

        return new DeferredJob()
        {
            DeferredList = deferredList.AsDeferredJobArray()
        }.Schedule(deferredList, 32, inputDeps);
    }
}
1 Like

Thank you, I was getting stuck on getting my EntityQuery to a NativeList. Was hoping for something similar to EntityQuery.ToComponentDataArray but for NativeList (or IbufferComponent to NativeList)

So if noting like this exist do I need another job to pass from a component array to a nativeList ?

Depend on case. If you using EQ and know buffer before schedule, why you need that? Why just not get buffer in OnUpdate and schedule job with this buffer length? Explain your case a bit more detailed :slight_smile: IJobParallelForDefer needed if you don’t know data length at scheduling time (for example previous job accumulate some data array under certain conditions), but in your case (I relates on your OP code) you have data already, you know it length (you can get data arrays, buffers etc fro EQ), you not changing this data and ranges in previous job.