Hi, i am a beginner in unity dots and in general but i am focusing on learning dots. The problem that i encountered is that i cannot create NativeArrays of any kind of allocator inside jobs.
Entities.WithAll<BowTag>().ForEach((Entity entity, in Translation translation, in Rotation bowRotation) =>
{
NativeArray<quaternion> rotations = new NativeArray<quaternion>(1, Allocator.Temp);
NativeArray<float3> translations = new NativeArray<float3>(1, Allocator.Temp);
NativeArray<float3> results = new NativeArray<float3>(1, Allocator.Temp);
rotations[0] = bowRotation.Value;
translations[0] = translation.Value + displacement - bowDisplacement;
CalculateQuaternionAndFloat3MultiplicationJob job = new CalculateQuaternionAndFloat3MultiplicationJob
{
rotation = rotations,
point = translations,
result = results
};
JobHandle jobHandle = job.Schedule(1, 1);
jobHandle.Complete();
commandBuffer.AddComponent<MovementData>(entity, new MovementData { speed = speed, target = results[0] });
rotations.Dispose();
translations.Dispose();
results.Dispose();
}).WithoutBurst().Run();
displacement and bowDisplacement are float3 values outside of the Entities.ForEach
I have tried to do some workarounds but all failed. As you see I hoped non burst compiled jobs can create at least Allocator.Temp but they cannot.
Can someone give me a way to actually to do this? (Preferably with burst compiled version) Thank you in advance.
You need to create the native arrays outside of the job and then pass them in to be used. In your case (where you call a job from within another job), you should be able to re-use them no?
It does not let me, it says
The managed class type System.IntPtr is not supported. Loading from a non-readonly static field Unity.Jobs.IJobParallelForExtensions.ParallelForJobStruct1.jobReflectionData` is not supported
NativeArray<quaternion> rotations = new NativeArray<quaternion>(1, Allocator.TempJob);
NativeArray<float3> translations = new NativeArray<float3>(1, Allocator.TempJob);
NativeArray<float3> results = new NativeArray<float3>(1, Allocator.TempJob);
commandBuffer.SetComponent<MovementData>(playerEntity, new MovementData { speed = speed, target = displacement });
Entities.WithAll<BowTag>().ForEach((Entity entity, in Translation translation, in Rotation bowRotation) =>
{
rotations[0] = bowRotation.Value;
translations[0] = translation.Value + displacement - bowDisplacement;
CalculateQuaternionAndFloat3MultiplicationJob job = new CalculateQuaternionAndFloat3MultiplicationJob
{
rotation = rotations,
point = translations,
result = results
};
JobHandle jobHandle = job.Schedule(1, 1);
jobHandle.Complete();
commandBuffer.AddComponent<MovementData>(entity, new MovementData { speed = speed, target = results[0] });
}).Run();
rotations.Dispose();
translations.Dispose();
results.Dispose();
AFAIK you can’t schedule a job inside a Entities.ForEach, you will need to do an Entities.ForEach to group the data needed for the job, then schedule the job with dependency on that ForEach.
I have a problem which i hope is going to be memory efficient using a job system. Let’s say i have a job which calls out to an end point remotely to get a Json string. which obviously varies in length. If i want to gain access to this Json string, do i need to allocate a native array outside of the job with a MaxSize allocation? And would i need to set the size of the JSon in another variable so i could get the String back outside of the job?
Questions about this (assuming i’m doing this correctly)
Would this max allocation of this native array be wasteful in terms of multiple calls to the job would require allocating this max array everytime?
Would there be a far better approach to calling endpoints in a multi-threaded engagement that did not have these memory setup issues?