I made a custom script that is a baked FollowerEntity that has no gameobjects associated, but can’t seem to get the Path Tracer to initialize properly which causes the ManagedState.Clone() to throw invalid allocation error.
My ISystem seems to handle allocating the PathTracer in builds, just not the editor.
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial struct ManagedStateSetupSystem : ISystem
{
void OnUpdate(ref SystemState state)
{
var world = World.DefaultGameObjectInjectionWorld;
NativeList<Entity> removeEntities = new NativeList<Entity>(Allocator.Temp);
foreach (var (managedState, entity) in SystemAPI.Query<ManagedState>().WithAny<ManagedStateInit>().WithEntityAccess())
{
if(managedState == null)
{
Debug.LogError("Missing managed state");
continue;
}
//The path tracer gets disposed in the authoring component we need to create it
if (!managedState.pathTracer.isCreated)
{
managedState.pathTracer = new PathTracer(Allocator.Persistent);
world.EntityManager.SetComponentData(entity, managedState);
}
removeEntities.Add(entity);
}
//Clear the init tag
world.EntityManager.RemoveComponent(removeEntities.AsArray(), typeof(ManagedStateInit));
removeEntities.Dispose();
}
}
@aron_granberg For now I hacked in a fix to ManagedState.Clone() that might be worth including as a safety net. This is probably better than using that ISystem.
object System.ICloneable.Clone () {
//Just incase the pathTracer isn't allocated yet
if (!pathTracer.isCreated)
{
pathTracer = new PathTracer(Unity.Collections.Allocator.Persistent);
}
return new ManagedState {
#pragma warning disable 618
autoRepath = autoRepath.Clone(),
#pragma warning restore 618
pathTracer = pathTracer.Clone(),
rvoSettings = rvoSettings,
pathfindingSettings = new PathRequestSettings {
graphMask = pathfindingSettings.graphMask,
tagPenalties = pathfindingSettings.tagPenalties != null ? (int[])pathfindingSettings.tagPenalties.Clone() : null,
traversableTags = pathfindingSettings.traversableTags,
traversalProvider = null, // Cannot be safely cloned or copied
},
enableLocalAvoidance = enableLocalAvoidance,
enableGravity = enableGravity,
onTraverseOffMeshLink = null, // Cannot be safely cloned or copied
pendingPath = null, // Cannot be safely cloned or copied
activePath = null, // Cannot be safely cloned or copied
};
}
Has anyone figured another way to initialize the ManagedState.PathTracer using an authored FollowerEntity so that it works in the Editor?