Baked Follower Entity Path Tracer Issue

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?

Hi

The only think you should have to do is

managedState.pathTracer = new PathTracer(Allocator.Persistent);

That ManagedState.Clone throws an exception when the path tracer is not initialized is a bug, however. I will fix that in the next update.

1 Like

I tried doing that in the Authoring script but it seems to get disposed automatically when it’s converted to an Entity. That is why I made the ManagedStateSetupSystem, but even that doesn’t seem to work in the editor because ManagedState.Clone is called before the system assigns the PathTracer.

Is the Clone function just an editor thing? Is that why it only throws the allocation error when running in Editor?

Check the new update. In 5.2 the Clone method should no longer throw an exception if the PathTracer is uninitialized.

1 Like

Awesome! The update is working good so far :slight_smile: