I’ve got a game with a maze, and an NPC that can jump walls if it finds a shorter path to its destination on the other side of a wall. My goal is to construct paths for each eligible neighbor cell and compare their lengths. If one of the paths from a neighbor cell is shorter than the current path, my NPC will jump the wall and start on that shorter path.
Problem is, the PipelineState for the ABPaths I construct never leave the Created state. IsDone() is never true, and the coroutine is stuck in the second while loop. Am I missing something? I can’t find documentation for starting the process of constructing the individual paths, and it looks like the ABPath.Construct() function should do that already. Is there an issue with calling that function in a coroutine for some reason? I’m at a loss.
Code:
IEnumerator AttemptJump()
{
pathList.Clear();
jumpCandidateCell = null;
// Check each neighbor cell and construct a path to the destination for eligible paths
MazeCell currentCell = wandering.CurrentCell;
if (NeighborCanBeJumpedTo(currentCell.RightNeighbor, 0))
pathList.Add(ABPath.Construct(currentCell.RightNeighbor.Intersection.transform.position, aiPath.destination));
if (NeighborCanBeJumpedTo(currentCell.TopNeighbor, 1))
pathList.Add(ABPath.Construct(currentCell.TopNeighbor.Intersection.transform.position, aiPath.destination));
if (NeighborCanBeJumpedTo(currentCell.LeftNeighbor, 2))
pathList.Add(ABPath.Construct(currentCell.LeftNeighbor.Intersection.transform.position, aiPath.destination));
if (NeighborCanBeJumpedTo(currentCell.BottomNeighbor, 3))
pathList.Add(ABPath.Construct(currentCell.BottomNeighbor.Intersection.transform.position, aiPath.destination));
// Stop attempt if no eligible paths
if (pathList.Count == 0)
yield break;
// Wait for all paths to be calculated
float shortestLength = aiPath.remainingDistance;
while (pathList.Count > 0)
{
while (!pathList[0].IsDone())
{
Debug.LogFormat("Waiting: {0}", pathList[0].PipelineState.ToString());
yield return null;
}
// Check if constructed path is shorter than shortest path
float pathLength = pathList[0].GetTotalLength();
if (pathLength < shortestLength)
{
shortestLength = pathLength;
jumpCandidateCell = Maze.GetCellAtPos(pathList[0].originalStartPoint);
Debug.LogFormat("{0} is shorter!", jumpCandidateCell.name);
}
else
Debug.LogFormat("{0} is not faster than the shortest path", Maze.GetCellAtPos((Vector3)pathList[0].path[0].position).name);
// Remove path from the list
pathList.RemoveAt(0);
}
if (jumpCandidateCell != null)
StartJumping();
}
Thank you!