Maybe i am just very greedy in terms of line count, but in MonoBehaviour workflow i always tried to condense all often used logic used in a system to 1 line. e.g. Objects objects = UtilityClass.GetAllObjectsWithAttributes(myAttributes), or
Vector4[ ] complicatedMathResult = UtilityClass.DoComplicatedMath(prerequisiteOne, prerequisiteTwo, prerequisiteThree) Where the UtilityClass is tucked away in a file somewhere else.
Jobs makes this at the very cheapest a 3 line thing with e.g.
NativeArray<Vector4> result = new NativeArray<Vector4>(Allocator.TempJob);
new ComplicatedMathJob(prerequisiteOne, prerequisiteTwo, prerequisiteThree, result).Run();
result.Dispose();
of course the 3rd line being memory management, which makes sense for the DOTS benefits.
Thing is, i can move this outside the OnUpdate(), as long as its still in the ISystem {} block.
/// <summary>
/// <para>Runs a job that does complicated math</para>
/// </summary>
private Vector4[] DoComplicatedMath(DataType prerequisiteOne, DataType prerequisiteTwo, DataType prerequisiteThree, ref SystemState state)
{
// Create some natives
NativeList<Vector4> nativeMath = new NativeList<Vector4>(Allocator.TempJob);
// Do the actual math
new ComplicatedMathJob(prerequisiteOne, prerequisiteTwo, prerequisiteThree, nativeMath).Run();
// Convert the natives
Vector4[] result = UtilitiesNotClassButStructIguess.ConvertFromNative(nativeMath);
// Dispose the natives
nativeMath.Dispose();
// Return the result
return result;
}
Enabling me to do things like
Vector4[] complicatedMathResult = DoComplicatedMath(prerequisiteOne, prerequisiteTwo, prerequisiteThree, ref state), which is one line i can put wherever i need.
The problem here is, for each unique function, my ISystem {} block grows thicker. Personally i dont see the difference between having my function be inside a ISystem {} block, or some other files utility struct, but that might just be my limited understanding of C# and ECS
I guess in the end, the overhead is not that big, as you can declare your native prerequisites (or fake aspect) once, then run and use the job a couple times, to dispose it at the bottom of OnUpdate().
NativeList<DataType> result = new NativeList<DataType>(Allocator.TempJob)
new SomeJob(prerequisiteOne, prerequisiteTwo, prerequisiteThree, result).Run();
DoThingsWithResult(result);
prerequisiteOne = ChangePrerequisiteOne(prerequisiteOne);
prerequisiteTwo = ChangePrerequisiteTwo(prerequisiteTwo);
prerequisiteThree = ChangePrerequisiteThree(prerequisiteThree);
new SomeJob(prerequisiteOne, prerequisiteTwo, prerequisiteThree, result).Run();
DoThingsWithResult(result);
prerequisiteOne = ChangePrerequisiteOne(prerequisiteOne);
prerequisiteTwo = ChangePrerequisiteTwo(prerequisiteTwo);
prerequisiteThree = ChangePrerequisiteThree(prerequisiteThree);
new SomeJob(prerequisiteOne, prerequisiteTwo, prerequisiteThree, result).Run();
DoThingsWithResult(result);
result.Dispose();
For which calling the job is like calling a function.