The path has an invalid state? Expected created found Returned

Hey There!

I recently installed the latest version of AStar Pathfinding Project. I am using a point graph for my flying spaceships. At one point, all they have to do is to follow the nodes, which are part of a patrol route. But it keeps throwing this error

"The path has an invalid state. Expected created found Returned
“Make sure you are not requesting the same path twice”.

I dont understand what’s happening here. It was working fine on the previous version. Also, has the ability to create a ‘new’ instance of a Path been removed. If so, I think this might be causing the problem for me.

Here is the problematic code:

void Convoy_EnterState()
{

  if(IsLeader)
  {
  	RouteNodes = new Transform[Route.childCount];
  	i = 0;
  	foreach(Transform t in Route)
  		RouteNodes[i++] = t;
  	
  	RouteNodesIndex = 0;
  	targetReached = false;

// RouteNodesIndex = 0;
currentWaypoint = 0;

// SetPath();
// Debug.DrawLine(tr.position, RouteNodes[0].position,Color.red);

  	path = GetPath(tr.position, Vector3.zero, OnPathComplete);

// vPath = path.vectorPath;
// pathLength = vPath.Count;
}
else
{
//empty for now
}

}

void SetPath()
{
Debug.Log(“Here?”);
targetReached = false;
RouteNodesIndex = RouteNodesIndex + 1;
currentWaypoint = 0;

  if(RouteNodesIndex >= i)
  {
  	currentState = States.Gaurd;
  }
  path = GetPath(RouteNodes[RouteNodesIndex-1].position, RouteNodes[RouteNodesIndex].position, null);

}
void Convoy_Update()
{

  if(IsLeader)
  {
  	Debug.Log("Here?");

// vPath = path.vectorPath;
// pathLength = vPath.Count;

  	if(path == null)
  		return;
  	if(currentWaypoint > path.vectorPath.Count)
  		return;
  	if(currentWaypoint == path.vectorPath.Count && currentWaypoint != 0)
  	{								
  		targetReached = true;
  		SetPath();
  	}
  	Debug.DrawLine(tr.position, path.vectorPath[currentWaypoint],Color.red);
  	if(!targetReached)
  	{
  		targetForPatrol = path.vectorPath[currentWaypoint];
  	}
  	base.Chase (targetForPatrol);
  	if(SqrDistanceToTarget < nextWaypointDistance * nextWaypointDistance && !targetReached)
  		currentWaypoint++;
  	//			if(targetForPatrol == PatrolPoints[PatrolPoints.Count-1].position)
  }
  else 
  {
  	FollowConvoy(groupLeader.position + groupLeader.TransformDirection(offset));
  }

}

Thanks!

Hi

It’s hard to say without seeing the implementation for GetPath, but likely what happens is that you are creating one instance of a Path object and then calling StartPath with that path instance multiple times.

Yeah, using the constructor to create a path instance has been discouraged for a long time now. You should use the static methods called “Construct”. They are better since they can handle pooling of paths which helps with memory management. E.g you would do

var myPathInstance = ABPath.Construct(start, end);
Seeker.StartPath (myPathInstance, OnPathComplete);

And of course you can still do the more compact

Seeker.StartPath(start, end, OnPathComplete);

I solved my problem using the Construct function. But another one has cropped up! Do you know the causes for getting a NullReferenceException on this statement
path = ABPath.Construct (tr.position, GoToPosition, null);
Both the above variables are not null, as I examined in the inspector during play mode.

If they are both not null, then you cannot be getting a null reference exception from that line.
So I recommend that you use either a debugger or write something like

Debug(tr != null ? "Tr is not null", "Tr is null");

above that line.