A* Pathfinding Project

"The specified path has not been started yet." and delegates?


#1

So I’m trying to create a simple turn based Xcom Style movement grid, I’ve found a list of nodes in a bounds area and used ABPath.Construct to create a path to each node it found. Problem is the paths returned have a node count of 0. I’m assuming it’s because I’m trying to read the path before it’s calculated but I’m not sure how to write the OnPathDelegate callback and if I use BlockUntilCalculated I get a “The specified path has not been started yet.” Error. Here is my code.

void CreateMovementGrid(Unit unit)
    {
        GridGraph graph = AstarPath.active.graphs[0] as GridGraph;

        bounds.center = unit.transform.position;
        bounds.size = new Vector3(25, 10, 25);
        List<GraphNode> nodes = graph.GetNodesInRegion(bounds);

        List<ABPath> paths = new List<ABPath>();
        for (int i = 0; i < nodes.Count; i++)
        {
            if (nodes[i].Walkable)
            {
                //Debug.DrawLine((Vector3)nodes[i].position, (Vector3)nodes[i].position + (Vector3.up), Color.black, 50);
                var p = ABPath.Construct(unit.transform.position, (Vector3)nodes[i].position);
                p.BlockUntilCalculated();
                paths.Add(p);
                
            }
        }   
    }

    public void PathCalculated()
    {

    }

#2

Hi

You create the path request, but you never tell the pathfinding system to start to search for a path.
Try this instead:

...
var p = ABPath.Construct(unit.transform.position, (Vector3)nodes[i].position);
AstarPath.StartPath(p);
p.BlockUntilCalculated();
paths.Add(p);
...

See also https://arongranberg.com/astar/docs/callingpathfinding.html

You may also want to check out the example scene called ‘Turnbased’ (only available in the pro version though).


#3

Ah yes, that worked thanks =)

Still unsure how to write the callback,

ABPath.Construct(unit.transform.position, (Vector3)nodes[i].position, PathCalculated);

This complains saying cannot convert from “method group” to “OnPathDelegate”.

I’m assuming the callback triggers when the path has been calculated. Also if so what would be the difference to using that rather than BlockUntilCalculated?


#4

Make sure that the PathCalculated method takes a Path object as the only parameter. There are some examples on the page I linked.
Path requests are normally asynchronous, if you call BlockUntilCalculated you force the system to calculate the path immediately.


#5

Ah right, I was assuming it took a ABPath not a Path. Thank you very much for your help =)