UpdateGraph with Multiple GraphUpdateObjects and Proper way to cancel path search

Hey there,

Working on TD game, there is a functionality that Player can sell multiple towers at a time. Those multiple towers could be selected freely so I can’t merge them as a larger bound. I need to create separated GUOs then update graph with those objects, simply my code looks like;

foreach(var tower in sellList) {
        ...
	GraphUpdateObject guo = new GraphUpdateObject(guoBounds);
	guo.addPenalty = -1000000;
	guo.resetPenaltyOnPhysics = false;
	guo.requiresFloodFill = false;
	pathFinder.UpdateGraphs(guo);
}

I also subscribed AstarPath.OnGraphsUpdated callback in various classes which calculate new path for creeps, so calling “pathFinder.UpdateGraphs(guo);” in a loop is lame, as you expected at some point Astar will try to recalculate a new path before complete early request, thus I’m getting ;

“Canceled path because a new one was requested …”

I wonder if there is a way to put those GUO objects in a queue then call UpdateGraph, so far I didn’t find anything on documentation.

At this point I can add a function into AstarPath.UpdateGraphs(List<GraphUpdateObject>) and call that function after loop, this should solve the problem, Actually this question is going to @aron_granberg directly does it make sense to you ?

My other Question is also related to “Canceled path because a new one was requested” error. Whenever player adds a new Tower I’m going to update graph and those my own PathFinder classes start to compute a new path for creeps if Player adds a new tower again while calculating new path I’m getting that error as usual. At that point I don’t need to old Path which is obsolete I don’t wanna wait to complete old path result and send new Path request, what is the proper way to cancel Path request ?

Thanks for your time

Hi

UpdateGraphs will internally put the graph updates in a queue and then execute them in a batch as soon as the pathfinding threads have been stopped (likely the next frame) (also see http://arongranberg.com/astar/docs/class_astar_path.php#aae2403939ee3da2029ad5e80042dc8c3). The OnGraphsUpdated callback should only be called once for all those calls.

You get the “canceled path because a new one was requested” message if you call StartPath before the current path had time to be calculated. You can check if the seeker is done by calling the seeker.IsDone method.
You can cancel a path manually by calling path.Error() on it, however this is essentially the same thing that the seeker does when it gets a new path request before it had time to calculate the previous one. If you want to turn off path result logging (which cleans up the log as well as being more performant) you can disable it in A* Inspector -> Settings -> Path Log Mode. Calling the StartPath or Error method before the previous path has been calculated it totally fine as long as you are aware that you will have wasted some time calculating the previous path.

2 Likes

hi,

Oh you’re right, I forgot to remove one of my old event which is cause to call OnGraphsUpdated callback twice. And ignoring those errors completely fine in my case. Thanks you so much.

1 Like