Getting the length after seeker.StartPath()

Hi, I just started using your A* Pathfinding Project and I’m having trouble getting the length of the path.

I’m using a point graph to map over a hex grid which at this point seems to be working except for one problem. I need to get the length of the path back to calculate resources spent moving the unit. The first move works fine, but if I double back, retracing the move, it sometimes reports (via log) 1 less length. If it was consistent I could work around this, but it seems to be rather random on if it does this or not. For example, moving one hex usually reports back length 2, but sometimes, when backtracking, it reports back 1.

Thanks

Hi

How are you getting the length of the path right now?

I’m getting the length reported back from StartPath() in the debug log. I can get the int with:

onPathComplete(Path p)
Debug.Log("log count: " + p.path.Count)

I get this when first moving
Path Completed : Computation Time 0.00 ms Searched Nodes 3 Path Length 3
Path Number 10 (unique id)
log count: 3

and this when back tracking to the same hex
Path Completed : Computation Time 0.00 ms Searched Nodes 2 Path Length 2
Path Number 11 (unique id)
log count: 2

Depending on the StartEndModifier you’re using, the Pathfinder may be adding a waypoint in the first example to get the path “aligned” with the grid. If your entity is starting off just a fraction of a unit off-grid from the first “true” graph node, it might be creating two of them very close together at the beginning. You could debug what the Pathfinder has calculated like this:

onPathComplete(Path p) {
    // Assuming no errors...
    foreach (Vector3 position in p.vectorPath) {
        Debug.Log("Waypoint: " + position);
    }
}

That’ll help you see if there’s two waypoints very very close together, like:

Waypoint: (2.00001, 3.0, 0.0)
Waypoint: (2.0, 3.0, 0.0)
Waypoint: (1.0, 3.0, 0.0)

That sort of thing.

I added that Debug you suggested and this is what I’m getting back after moving 1 hex.

Path Completed : Computation Time 0.99 ms Searched Nodes 2 Path Length 2
Path Number 15 (unique id)
Waypoint: (-0.9, 0.0, 1.0)
Waypoint: (0.9, 0.0, 1.0)
Path Completed : Computation Time 0.00 ms Searched Nodes 0 Path Length 1
Path Number 16 (unique id)
Waypoint: (-0.9, 0.0, 1.0)
Waypoint: (-0.9, 0.0, 1.0)

I think you are on to the problem, it does seem to add an extra waypoint.

Hi

As @torgie mentions, this is caused by the StartEndModifier. The path will be post-processed to tweak the first and last points in the path so that they are at the exist points used in the path query (which may not line up with the node positions). During this procedure it will ensure that the vectorPath list has at least two elements as otherwise it wouldn’t be able to modify the first and last points of the path independently. If you want the raw list of nodes I recommend that you check the ABPath.path list instead of the vectorPath list. That list is straight up the raw list of nodes the path traversed.

onPathComplete(Path p) {
    // Assuming no errors...
    foreach (GraphNode node in p.path {
        Debug.Log("Waypoint: " + (Vector3)node.position);
    }
}

Your first path log that you posted does however seem to actually cross two nodes, the waypoints are at different positions. The second one looks like it is just in one node however.
The Seeker will also draw the last path it calculated using a green polyline in the scene view. So if something seems odd you can view it there.