I have the following code:
void Start()
{
NativeArray<int> test = new NativeArray<int>(200, Allocator.Persistent);
int jobCount = 2;
NativeArray<JobHandle> jobArray = new NativeArray<JobHandle>(jobCount, Allocator.Temp);
for (int i = 0; i < jobCount; i++)
{
FindPathJob findPathJob = new FindPathJob {testArray= test};
jobArray[i] = findPathJob.Schedule();
}
JobHandle.CompleteAll(jobArray);
jobArray.Dispose();
test.Dispose();
}
[BurstCompile]
private struct FindPathJob : IJob
{
public NativeArray<int> testArray;
public void Execute(){
//do stuff, never access/read/write testArray
}
}
For jobCount > 1 this throws an error saying
InvalidOperationException: The previously scheduled job Pathfinding:FindPathJob writes to the Unity.Collections.NativeArray1[System.Int32] FindPathJob.testArray. You are trying to schedule a new job Pathfinding:FindPathJob, which writes to the same Unity.Collections.NativeArray
1[System.Int32] (via FindPathJob.testArray). To guarantee safety, you must include Pathfinding:FindPathJob as a dependency of the newly scheduled job.
And now im wondering where my error is. Essentially what i would like to do is to pass a list of pathfinding information (e.g. which tiles are blocked and so on) to this Pathfinding job. And since BurstCompiling doesnt allow reference values i have to create it beforehand, because else i cant really access my game grid.
However just passing a test native array and never even doing anyhting with it throws the above error. Is it possible without dependencies (as these would make the point of multithreading this pointless), that is to say is there a way to pass a job a threadsafe readonly argument?