Hello Community,
am trying to perform IJobParticleSystem jobs on two different particle systems but with both instances of the IJobParticleSystem writing to the same data structure.
I do not mind if both instances are always executed one after another (obviously, since otherwise we had race conditions).
However I do not seem to be able to get rid of the error:
InvalidOperationException: The previously scheduled job AirpocketsManager:AbsorbInPocketsTask writes to the Unity.Collections.NativeList`1[System.Single] AbsorbInPocketsTask.airpockets_data. You are trying to schedule a new job AirpocketsManager:AbsorbInPocketsTask, which writes to the same Unity.Collections.NativeList`1[System.Single] (via AbsorbInPocketsTask.airpockets_data). To guarantee safety, you must include AirpocketsManager:AbsorbInPocketsTask as a dependency of the newly scheduled job.
My code looks as follows:
This is in an initialization function, called on a list of all particle systems (which are just two for testing):
foreach (ParticleSystem system in all_systems)
{
AbsorbInPocketsTask task = new AbsorbInPocketsTask();
task.native_array = the_same_native_array;
airpockets_absorption_tasks.Add(system, task);
}
And this is the main part of the class:
Dictionary<ParticleSystem, JobHandle> airpockets_absorption_handles = new Dictionary<ParticleSystem, JobHandle>();
Dictionary<ParticleSystem, AbsorbInPocketsTask> airpockets_absorption_tasks = new Dictionary<ParticleSystem, AbsorbInPocketsTask>();
ParticleSystem last_system = null;
public void StartJobForSystem(ParticleSystem system)
{
if (last_system == null) // to ensure the very first call is not called without an existing system
airpockets_absorption_handles[system] = airpockets_absorption_tasks[system].Schedule(system);
else
airpockets_absorption_handles[system] = airpockets_absorption_tasks[system].Schedule(system, airpockets_absorption_handles[last_system]);
last_system = system;
//airpockets_absorption_handles[system].Complete();
}
StartJobForSystem() is called from the OnParticleUpdateJobScheduled() function of the two particle systems.
For testing purposes I did use two Dictionaries to go absolutely sure that each ParticleSystem gets its own instance of the job and its own JobHandler.
Using the second parameter of Schedule I intend to enforce that the last (or more like “other”, since there are just two currently) Job has finished before the new one is started.
However this always gives the error above, unless that line is not outcommented which obviously makes the whole usage of the job system pointless.
Anyone happen to know what I am doing wrong?
Huge thanks in advance