Canceled path because a new one was requested?

Hi! I am making a grid based strategy game, and when I select a unit, I want to calculate how long each path is in a certain area (hard to explain in words). However, I am getting an error: Canceled path because a new one was requested.

Here is my code that is causing the error:

foreach(GameObject i in SelectedTiles){
Vector3 testTo = i.transform.position;
distanceTester.StartPath (transform.position, testTo, AfterTest);
}

Basically, for each object in the list Selected Tiles, I need to calculate a path to it, and then use .GetTotalLength() to find out how long it is. If it is larger than a certain length, it will be removed from the list of objects.

Please Help! Thanks!

Hi

The Seeker component is designed to be used for a single agent, and since agents only follow a single path at a time, it will cancel the previous path calculation if a new one is started. If you want to calculate multiple paths simultaneously you can bypass the Seeker by calling AstarPath.StartPath instead.

foreach(GameObject i in SelectedTiles){
    Vector3 testTo = i.transform.position;
    var path = ABPath.Construct(transform.position, testTo, AfterTest);
    AstarPath.StartPath (path);
}

See also http://arongranberg.com/astar/docs/calling-pathfinding.php#calling-directly

Aha, that makes complete sense! How would I reference each path to .GetTotalLength()?

In case this wasn’t clear: if the path to a certain tile is too long, I will remove it from the list of tiles.

Something like this maybe?

foreach(GameObject i in SelectedTiles){
    Vector3 testTo = i.transform.position;
    var path = ABPath.Construct(transform.position, testTo, (p) => {
           if (p.GetTotalLength()) {
               // Do something
           }
    });
    AstarPath.StartPath (path);
}

Also, you might be interested in these other path types
http://arongranberg.com/astar/docs/class_pathfinding_1_1_multi_target_path.php
http://arongranberg.com/astar/docs/class_pathfinding_1_1_constant_path.php
They are only available in the pro version however (you can test them in the “PathTypes” example scene).

Okay, it almost works now I think, but I’m having one error. I created two lists, tempSelectedTiles, and SelectedTiles, the foreach loop checks the length of the path to tempSelectedTiles and if it is within range, add the gameobject to Sekected Tiles (this might not be the most efficient way to do this).

My code looks like this:

foreach(GameObject i in TempSelectedTiles){
   	Vector3 testTo = i.transform.position;
	var path = ABPath.Construct(transform.position, testTo, (p) => {
          if (p.GetTotalLength() <= maxRange) {
		     SelectedTiles.Add(i);
		     ActionVis tileAV = i.GetComponent<ActionVis>();
		     tileAV.turnOn();
         }
    });
  	AstarPath.StartPath (path);
	}

However, this is only effecting the first tile that is calculated, not each tile in TempSelectedTiles. Any idea why this would be? It is only executing tileAV.turnOn() on the tileAV component of the first gameobject.

Hi

I am not completely sure about the semantics of C# here, but you might need to make a copy of the i variable like this

foreach(GameObject tmp in TempSelectedTiles){
    var i = tmp;
    ...
}

otherwise the delegate might capture the wrong variable.

Aha! That was indeed the issue! You are truly a hero! Thank you for all the help! (and for astar unity)

1 Like