# Path consistency. Manually editing path?

I’ve observed that when several possible paths have the same cost, the nodes chosen are not predictable. for instance, ABPath calculates this path for me:

Then I make a slightly longer path, with one more node, and I get this:

As you can see, the first step in the path changes from the previous calculation. I would like to know if there is a way to make these paths more predictable. Maybe I can manually compare the old path to the new to replace all the previous nodes with the ones already calculated, but can that be done safely?

Hi

Which of the possible path is chosen mainly depends on the heuristic that is used.
Take a look at the description for the heuristic field on this page for more examples: https://arongranberg.com/astar/docs/inspector.html#heuristic
In your case, I think the diagonal manhatten heuristic would work better. It will prefer to go diagonally as far as possible first, and then switch to axis aligned movement.

I tried, but still the paths are not consistent after calculating with 1 more node, no matter what heuristics I use. In my first example both use euclidean, and results are different. Diagonal Manhattan does the same.

Is my best bet to save the last calculated path, and then add an extra node at the end? what would be the best way to update a path without potentially causing trouble?

Yeah there are no heuristics that will always be consistent like that, especially considering it always calculates the shortest one. But you could as you say calculate a path to the first waypoint, and then concatenate it with the path from the first waypoint to the second (and so on).

Possibly you could do a check to see if the shortest path to the last waypoint is shorter than the concatenation of all previous paths, and if so you replace all previous paths with the shortest path.

Thanks for the replies so far. Can you maybe show me how to manually edit a path (adding a new node at the end) or concatenate paths in code?

Just to clarify what I need; If I add a node with Path.path.Add(), do I somehow need to recalculate vectorpath and other data inside the path for it to work correctly everywhere else?

Looking at the source code, the definition for Path.path is simply:

public List<GraphNode> path;

So adding/removing elements from the path List won’t affect the vectorPath List.

I’m facing something similar where I need to manually “teleport” entities around my game, so my approach will likely be to create a static helper class that manipulates Path objects:

using Pathfinding;
using UnityEngine;

public static class PathHelper {

public static void Add(Path path, GraphNode node) {
}

// Other useful Path manipulation methods

}

It looks like Path.GetTotalLength() calculates based off of the vectorPath, so that should still work after manually adding/removing points.

I’m not sure what drives the green path Gizmos in the editor, though… so if you add a bunch of elements to the path and vectorPath lists, they might work correctly but not show up in Unity. After a brief search I found RetainedGizmos, but… it seems like more trouble than it’s worth

Hope this helps!

1 Like

@torgie’s path helper would work perfectly.

Some things also use the Path.endPoint and Path.originalEndPoint fields, but for most things the above code should work well. For most purposes even just modifying the vectorPath field should be ok.

The Seeker (which draws the green path gizmos) will make a copy of the path and vectorPath lists when it has finished calculating a path, so any changes you make to the path object will not show up there.