How to ReUse same Path on different Seekers

Hi Just looking for some quick direction on how to achieve this. I’ve looked into Pooling and multipath but it doesn’t seem to cover this need

Scenario:
Currently Spawning ~200 seekers in quick succession and having them generate a path (with same start and end). Then each of them will apply a randomized vector modifier to their respective waypoints. The result is the Seekers are moving in a thick amalgamated column, sort of like a swarm instead of a straight line for the same path. Currently each path is getting calculated in ~40ms. I would like to re-use the original Path and remove all these extra calculations but I cannot seem to figure out how to correctly copy/reuse the path over.

I have tried GetNewPath to return an instance of it, but I think I don’t understand how it works exactly because i am still seeing the same calculation times.

I have also tried copying the path as new path with same contents, and then copying the vectorPath item by item as New vector, but somehow modifications still seem to affect the original Path.

Thank you

Hi

I think the easiest solution would be to use the FloodPath path type.

What the FloodPath type does is that it calculates the shortest path from every point on the map to a single target. After this has been done once you can use the FloodPathTracer path type to calculate a path from any point in the world to the target very quickly. This also allows you to spawn new agents from other positions on the map.

The PathTypes example scene contains a demo of how they are used.

Essentially you could use it like this

FloodPath fpath = FloodPath.Construct (someTargetPosition, null);
AstarPath.StartPath (fpath);

// Make sure the path is calculated immediately, just to make this code shorter
fpath.BlockUntilCalculated();

foreach (var seeker in seekers) {
    FloodPathTracer fpathTrace = FloodPathTracer.Construct (seeker.transform.position, fpath, null);
    // You may want to set the pathCallback field on the individual Seekers instead to make sure the correct scripts are notified
    // More about that here: https://arongranberg.com/astar/docs/calling-pathfinding.php
    seeker.StartPath (fpathTrace, OnPathComplete);
}

Thank you Aron, this looks like it will address my issue. Jut to confirm for the future however, there is nothing that will safely allow me to re-use a path on multiple agents?

Hi

It is possible to re-use the exact same path.
You can simply call the OnPathComplete methods in multiple movement scripts with the same path instance, or if necessary as in your case, you can construct a new path instance and set it to use a modified version of the first path result.

// In some global script
var path = seeker.StartPath(startPoint, endPoint);
path.BlockUntilCalculated();
foreach (var ai in allAIs) {
    var altPath = ABPath.Construct(startPoint, endPoint);
    altPath.vectorPath = new List<Vector3>(path.vectorPath);
    // Adjust altPath.vectorPath here to add that offset you talked about
    ai.OnPathComplete(altPath);
}

In the included movement scripts the OnPathComplete method is private, but you can make it public if you want to.