Why ai.SearchPath faster then ABPath.Construct

Hi

when I’m sending command to move for a lot of units
I’ve noticed that

public virtual void SetPath(Vector3 destination)
 {

            ai.destination = destination;
            ai.SearchPath();
}

works much faster than :

public virtual void SetPath(Vector3 destination)
    {
        PathCurrent = ABPath.Construct(AI.position, destination, OnPathComliteSetEndPoint);

        AstarPath.StartPath(PathCurrent);

        PathCurrent.BlockUntilCalculated();
    }

public virtual void OnPathComliteSetEndPoint(Path p)
    {
        if (p.error)
        {
            Debug.LogWarning("ABPath p.error - " + p.errorLog);
        }
        else
        {
            Seeker.PostProcess(p);

            AI.destination = p.vectorPath.GetLast();

            AI.SetPath(p);

        }
    }
  1. why?
  2. is it a way to add some performance to ABPath.Construct variation?

Hi

The most significant difference is that you call BlockUntilCalculated on the path. That will force the path to be calculated immediately, which is kinda bad for performance (e.g. no multithreading speedup if you do it for each path right after your create it). If you remove the BlockUntilCalculated call it should be faster.

Note that you can also just do

AI.canSearch = false;
PathCurrent = ABPath.Construct(AI.position, destination, OnPathComliteSetEndPoint);
AI.destination = destination;
AI.SetPath(PathCurrent);

If you call SetPath with a path that you haven’t calculated then the AI will just forward it to the Seeker component and calculate it itself. This has the benefit that AI.pathPending will work properly.

See https://arongranberg.com/astar/docs/iastarai.html#SetPath

1 Like

Wow, thank you! I’m gonna test everything

1 Like

Yep, it is awesome ! works much - much faster

1 Like