# Changing distance calculation for paths and checking cost of a path

I figure there is probably a cheap way to check the cost of a generated ABPath but looking through the docs I can’t find one. Is there? In particular I don’t want to just look at the total length as I want to get any penalty from diagonal and slopes.

Also, I can’t figure out where the distance cost is calculated for a path. I’ve been poking around all the relevant classes but I can’t find it. Using a constant path I generated this:

In that picture each square sits on a pathfinding node and that last row have an equal cost to reach from the origin. I want to figure out how to limit that so that the maximum achievable path is only on nodes on the same row as the originating node (e.g. if a path originates from origin 3,4 and has the cost to traverse 6 nodes it can only reach 9,4 but not 9,5 or 9,6 assuming a flat area).

Edit: At least a pointer to the right part of the documentation would be helpful. It seems like a simple problem but I can’t for the life of me figure out how to pull the G or H score of a node in a path even.

Something like “GetTraversalCost” sounds like what I am looking for but it doesn’t have any documentation and every time I pull it it returns 0. Looking at the code I would expect at least the node.Penalty to return something.

Hi

The length of a path in the sense you seem to want it is identical to the number of nodes in the path. That will be given by mypath.path.Count. Each element in the “path” list is a node the path passes through.

The distance costs are calculated at a lot of places. Mainly they are configured in the graph generator which calculated the graph. In this case the grid graph, which uses GridGraph.SetUpOffsetsAndCosts to set up costs. It keeps only a single copy of the distances between neighbouring nodes for memory reasons, most other graph types store the distance directly in the node (using the GraphNode.AddConnection method).

If you want, you can edit the GridGraph.SetUpOffsetsAndCosts method to use the same cost for diagonal and straight moves, then when you have calculated the path, you can just chop it off after a specified number of nodes to get a path which has a maximum length.

Didn’t quite get how you wanted to constrain the possible paths with rows…

I didn’t explain myself very well. If I have slope penalties and higher diagonal cost movement penalties than the “cost” of a path is not equal to the number of nodes right? Or am I misunderstanding that? If not, is that “cost” stored anywhere for a given path?

Hi

Yes, only if the diagonal costs and straight costs are equal and if there are no other costs from anything, the cost will be proportional to the number of nodes in the path.

After the path has been completely calculated, the cost information is discarded and recycled for other path requests (it requires a very large data structure). So after a path has been calculated you cannot directly get the total cost of it.

Thanks for clarifying.