Total Path Cost post modifiers on Layered Grid Graph

  • A* version: [5.4.4]
  • Unity version: [6.2]

I am using a Layered Grid Graph and am trying to build a movement preview. For that i calculate a path between two points on my grid (agent and target) and then apply a funnel modifier.
At that point i need to check the viability of the path. For that i need to calculate the total modified cost of the path (ie some areas might take twice as much movement, think difficult terrain in Baldurs Gate).
I have seen a method for the total length of the path, but haven’t found anything that would help me with this?
How would i approach this problem?

Also, side question, is there any way to let an agent calculate a path without it being automatically forwarded to the AiLerp component on the same object?

As in, you’re wanting to get the cost between two parts of a path? You may want to iterate through your points and check their GraphNode.penalty and use that per-node value to calculate the change? I’m assuming you’re trying to calculate the change within a specific area? Or are you specifically wanting a total cost of the entire path calculated?

Check the code on this part of the “Searching for paths” page.

Sounds like you’ll need to manually iterate through the funnel-modified path and accumulate the cost based on each node’s movement penalty—A doesn’t expose that directly. And yes, you can calculate a path without sending it to AiLerp by calling the seeker’s path request and handling the result yourself instead of assigning it to the movement component.*

I need it a bit more precise than that. I have a movement preview that prints the final path as a line. Two problems arise:

The first issue is the easier one. I need to calculate the Resource (AP / Action Points Cost) Cost of the clicked path. For paths without any penalties its relatively easy, since its just
AP_Cost = len(path) x AP_per_len.
However when traversing nodes that have an increased modifier (ie 1.5x) that changes. A very trivial approach is the following:
Cost = Sum over path nodes { node_penalty x AP_per_len x len(path) / amount_of_nodes }.
This works fine if taking the raw path before the funnel is applied since the path is basically evenly distributed among each node square.
However after the funnel is applied and the path is refined the path length per node area is no longer constant. So while the previous equation is still a decent rough model i would really like a more precise value. So i need to know how long the path segment through any given node is after the funnel is applied (For some nodes it might only cut through the corner of the node area for example).

The second issue is the movement preview itself. Similar to Baldurs Gate 3 I want the preview line to turn red for the parts of the path that are more costly, For that I need the precise entry and exit points for path segments through penalized nodes (From which using node adjacencies I could create sub-paths of same penalty and render from there)

I’m still a bit lost on how to solve this issue, so any further help is greatly appreciated.

I did some quick checking and there’s a method for something similar- GetTotalLength I didn’t check if this would take penalties into account (it’s for length though, not cost, so I imagine no) but in the documentation it says:

Total length of vectorPath, if vectorPath is null positive infinity is returned.

So you could totally apply the cost between the vectorPath nodes yourself and make your own version of this method. It even accounts for modifiers– I double checked that myself with the Funnel modifier.

seeker.StartPath(transform.position, aipath.destination, CheckPath);

void CheckPath(Path p){
    Debug.Log(p.GetTotalLength());
}

This doesn’t work for your use case? I read both of your points but I’m not connecting the dots on what may be missing.