How to limit searched for path length

Hi, I would like to know, how would I go about limiting the length of the parth seekers should be searching for?
For example, I have a player and an enemy standing next to each other, but they have some gap or obstacle between them. Normally I want the enemy to find its way to the player when they are nearby to go attack him or whatever. But sometimes, he might need to go around the whole map to reach the player if ‘the barrier between them is long enough’. At that point, I don’t want the enemy to go anywhere because it would look weird. Also, I could save quite some time on calculating the path, when all the potentially shortest paths are already longer than the requested limit.
How is this possible? I’ve been looking in the documentation and didn’t find out how. I would expect this functionality to be a part of Seeker.StartPath(…)?
Or is this for some reason not possible with your plugin? Do I have to always find the whole, no matter if I will be interested in it or not, and then throw it away when I manually calculate its length and find out it’s too long? That would be cruel. Please tell me you can limit the search somehow :-X Thank you

Hi

The default path type does not have this functionality built in unfortunately. You can as you say easily throw away the path if it is too long.

You can get this functionality working using an ITraversalProvider and changing 1 line of code though:

public class MyCustomTraversalProvider : ITraversalProvider {
    public int maxFScore = 1000 * 10;

    public bool CanTraverse (Path path, GraphNode node) {
        if (path.pathHandler.GetPathNode(node).F > maxFScore) return false;
        return DefaultITraversalProvider.CanTraverse(path, node);
    }

    public uint GetTraversalCost (Path path, GraphNode node) {
        return DefaultITraversalProvider.GetTraversalCost(path, node);
    }
}

You will have to open the Path.cs file and change the line

protected PathHandler pathHandler;

to

public PathHandler pathHandler;

for this to work however.

Then you can assign this traversal provider to your path instance.

var path = ABPath.Construct(...);
var customProvider = new MyCustomTraversalProvider();
customProvider.maxFScore = 1000 * 15;
path.traversalProvider = customProvider;

[Additional notes from when this answer suggested using the G score instead of the F score, see the next post for more details]
The G score for a node is the total cost for moving from the starting node to that node. By default the cost of moving 1 world unit is 1000. What this code will effectively do is to make all nodes too far away from the starting node unwalkable. However this can have some unintended performance consequences. If the threshold is set to some high value but the player is further away than that the system will have to search all nodes inside of the distance threshold before it can be sure that it is not possible to reach the player with a sufficiently short path. In many cases it may well be that calculating the whole path would have been much faster than having to look at all nodes within the distance threshold. This means that this is not necessarily an optimization. Be careful with large values of ‘maxGScore’.

Actually. In your case it is probably better to do a check against the F score instead.
The F score is the estimated total length of the path (and is usually calculated as G score + straight line distance to the destination).
Assuming your map does not contain any magic portals or things like that, it should work better than the G score and it does not have the same potential performance regression as the check with the G score does.

I have edited the code in my previous answer.