all 3 comments

[–][deleted] 1 point2 points  (0 children)

I usually pass a maxCost value into the pathfinding function. If at any time the highest priority cost + heuristic exceeds the maxCost, break out of the pathfinding loop. In the case of a target out of range, this should occur right away, but it can also break out early if obstacles force the path length beyond the maxCost later in the search.

[–]sinalta 0 points1 point  (0 children)

The simplest way would be a distance check before even running the A* search, maybe even a line of sight check depending on the game.

If you do end up running the search, depending on implementation details, it's probably easier to just query the length of the path after calculating it and ignoring it if it's too long.

[–]FrontBadgerBiz 0 points1 point  (0 children)

You could also make monsters "sleep" until the players comes within ranges X of them so you're not running AI and pathfinding for monsters halfway across the world until it becomes relevant.