I’ve tried this so far:
Code
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
[assembly: RegisterGenericComponentType(typeof(Edengrall.AI.ReduceSpeed<Food>))]
[assembly: RegisterGenericJobType(typeof(Edengrall.AI.DepleteNeedsSystem<Food>.ExpendJobWith))]
[assembly: RegisterGenericJobType(typeof(Edengrall.AI.DepleteNeedsSystem<Food>.ExpendJobWithout))]
namespace Edengrall.AI {
public struct ReduceSpeed<T> : IComponentData, AutoSaveIComponentData, IValue<float> where T : struct, IComponentData, AutoSaveIComponentData, IValue<float>, NeedComponent<T> {
public float Value;
float IValue<float>.Value { get => Value; set => Value = value; }
}
//public static class DepleteNeedsSystemFunctions {
// public static void CreateEntityQueries<T>(ref SystemState state, out EntityQuery WithSpeed, out EntityQuery WithoutSpeed) where T : struct, IComponentData, AutoSaveIComponentData, IValue<float>, NeedComponent<T> {
// }
//}
public struct DepleteNeedsSystem<T> : ISystemBase where T : struct, IComponentData, AutoSaveIComponentData, IValue<float>, NeedComponent<T> {
private EntityQuery WithSpeed;
private EntityQuery WithoutSpeed;
private ExpendJobWith JobExpendJobWith;
private ExpendJobWithout JobExpendJobWithout;
public void OnCreate(ref SystemState state) {
WithSpeed = state.GetEntityQuery(ComponentType.ReadWrite<T>(), ComponentType.Exclude<ReduceSpeed<T>>());
WithoutSpeed = state.GetEntityQuery(ComponentType.ReadWrite<T>(), ComponentType.ReadOnly<ReduceSpeed<T>>());
JobExpendJobWith = new ExpendJobWith();
JobExpendJobWithout = new ExpendJobWithout();
}
public struct ExpendJobWith : IJobChunk {
public ComponentTypeHandle<T> Need;
[ReadOnly] public ComponentTypeHandle<ReduceSpeed<T>> ReduceSpeed;
public float Time;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) {
var Need = chunk.GetNativeArray(this.Need).Reinterpret<float>();
var ReduceSpeed = chunk.GetNativeArray(this.ReduceSpeed).Reinterpret<float>();
for (int i = 0; i < chunk.Count; i++) {
if (Need[i] > 0) {
Need[i] -= ReduceSpeed[i] * Time;
if (Need[i] < 0) {
Need[i] = 0;
}
}
}
}
}
public struct ExpendJobWithout : IJobChunk {
public ComponentTypeHandle<T> Need;
public float Time;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) {
var Need = chunk.GetNativeArray(this.Need).Reinterpret<float>();
for (int i = 0; i < chunk.Count; i++) {
if (Need[i] > 0) {
Need[i] -= Time;
if (Need[i] < 0) {
Need[i] = 0;
}
}
}
}
}
public void OnDestroy(ref SystemState state) { }
[BurstCompile]
public void OnUpdate(ref SystemState state) {
JobExpendJobWithout.Need = JobExpendJobWith.Need = state.GetComponentTypeHandle<T>();
JobExpendJobWith.ReduceSpeed = state.GetComponentTypeHandle<ReduceSpeed<T>>();
state.Dependency = JobExpendJobWith.ScheduleParallel(WithSpeed, JobExpendJobWithout.ScheduleParallel(WithoutSpeed, state.Dependency));
}
}
}
Am I doing it wrong? How Wrong? Burst compilation crashed and burned and is now permanently compiling 329/330 functions. I also got this error messasge which might or not might be unrelated:
error
Unexpected exception System.InvalidOperationException: Could not find `burst.initialize` function in library '7FFC1E200000' ---> System.InvalidOperationException: Unable to load function entry `burst.initialize`
at Burst.Backend.UnmanagedLibrary+WindowsUnmanagedLibrary.GetFunctionByName (System.IntPtr libraryHandle, System.String functionName) [0x00025] in <b2dc5b19d1394c95a1a861711473d7d9>:0
at Burst.Backend.UnmanagedLibrary.GetFunctionByName (System.IntPtr libraryHandle, System.String functionName) [0x00000] in <b2dc5b19d1394c95a1a861711473d7d9>:0
at Burst.Compiler.IL.Jit.JitCacheLibraryManager.CallBurstInitializeFunction (System.IntPtr libraryPtr, Burst.Compiler.IL.Jit.GetExternalFunctionPointerDelegate getExternalFunctionPointer) [0x00062] in <f95137a4a8d747bbac814ce8830deb34>:0
--- End of inner exception stack trace ---
at Burst.Compiler.IL.Jit.JitCacheLibraryManager.CallBurstInitializeFunction (System.IntPtr libraryPtr, Burst.Compiler.IL.Jit.GetExternalFunctionPointerDelegate getExternalFunctionPointer) [0x000c5] in <f95137a4a8d747bbac814ce8830deb34>:0
at Burst.Compiler.IL.Jit.JitCacheLibraryManager+<>c__DisplayClass7_2.<TryGetOrLoadLibrary>b__1 () [0x00000] in <f95137a4a8d747bbac814ce8830deb34>:0
at Burst.Compiler.IL.Jit.JitCacheMethodValue.GetCachedPtrForAssemblies (System.Boolean ensureBurstInitializeHasBeenCalled, System.IntPtr& cachedPtr) [0x0004c] in <f95137a4a8d747bbac814ce8830deb34>:0
at Burst.Compiler.IL.Jit.JitCompiler.CompileMethods (System.ValueTuple`2[Mono.Cecil.MethodReference,System.String][] methodReferences, Burst.Compiler.IL.Jit.JitOptions jitOptions) [0x002c1] in <f95137a4a8d747bbac814ce8830deb34>:0
at Burst.Compiler.IL.Jit.JitCompiler.CompileMethod (Mono.Cecil.MethodReference methodReference, System.String methodRefString, Burst.Compiler.IL.Jit.JitOptions jitOptions) [0x0002c] in <f95137a4a8d747bbac814ce8830deb34>:0
at Burst.Compiler.IL.Jit.JitCompilerService+CompilerThreadContext.Compile (Burst.Compiler.IL.Jit.JitCompilerService+CompileJob job) [0x00464] in <f95137a4a8d747bbac814ce8830deb34>:0
While compiling job: System.Void Unity.Jobs.IJobExtensions/JobStruct`1<FindCloserValue`1/FindCloserValueJob<NPCFaceIcon>>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)
at <empty>:line 0
I actually don’t know if it’s a bug or I am doing something completely wrong. Also, shouldn’t there be a [assembly: RegisterGenericSystem(typeof(Edengrall.AI.DepleteNeedsSystem<Food>))]
?