A* Pathfinding Project

Get a vector3 from StartMultiTargetPath


#1

Small question about StartMultiTargetPath:

Right now i am using
seeker.StartMultiTargetPath(transform.position, postionArray, false, OnPathComplete);
Downside is that my unit immediately starts moving.

In my ideal situation “StartMultiTargetPath” would return the shortest path in a Vector3, that I could use with AiPath, someting like “ai.destination = path”

Is it possible to get to calculate the shortest path (without moving the AI), and return a vector3?

Thanks


#2

Hi

With the Seeker that is unfortunately not possible as currently the AIPath script listens for all path complete events on the Seeker. However you could do something like this:

MultiTargetPath p = MultiTargetPath.Construct(start, endPoints, null, null);
p.pathsForAll = false;
AstarPath.active.StartPath(p);

// ... wait for the path to be calculated, e.g. using p.BlockUntilCalculated() or p.WaitForPath()

// The calculated path is now in p.vectorPath (if it was successful, that is)
// Post process the path using the Seeker's modifiers (optional)
seeker.PostProcess(p);

// Prevent the agent from recalculating its path again, otherwise this path will soon be overridden by an automatic path recalculation to the destination
ai.canSearch = false;

// Make the agent follow the path
ai.SetPath(p);

See also the bottom of this page: https://arongranberg.com/astar/docs/callingpathfinding.html


#3

Thanks for the fast reply, on my way home I realised it makes no sense to calculate a path, and calculate it again using a Vector3.

I’m trying your code but got a error on AstarPath.active.StartPath(p);
I searched for solution on the forum and on the bottom of the page you referred, but sadly didnt find a solution.

Static member 'AstarPath....' cannot be accessed with an instance reference, qualify it with a type name instead

It does work withoud the “active” but then de code doens’t work :wink:


#4

The reason I am asking this, is because for some reason the code works perfectly with “ai.destination”, but not with the seeker (multible targets).

I’m using “ai.reachedDestination” to fire a callback, so the ai can start it’s next task. With the seeker this callback keeps getting fired after the first time reaching destination.

The ai.reachedDestination i still true when starting the next path. So the callback is called immediately, while the ai is stil traveling.

Previously this worked with ai.destination.


#5

Hi

Do you happen to have a custom variable called AstarPath or something?

reachedDestination uses the destination property. When you pass a custom path to the AI it will follow that but the destination is still whatever you set it to. I would recommend that you set the destination property to the endpoint of your multi target path to make things work as expected.

ai.SetPath(path);
ai.destination = path.originalEndPoint;

#6

No custom variable.
I only load private Seeker seeker; & private AIPath ai;

I added a screenshot.

I tried using your code withoud the AstarPath.active.StartPath(p); But then the “path.originalEndPoint;” is the 0.0, 0,0, 0.0


#7

ah, right. Sorry. It should be just AstarPath.StartPath.


#8

Also note that you need to wait for the path to be calculated (like I mentioned in a comment in the code).

E.g.

AstarPath.StartPath(p);
p.BlockUntilCalculated();

#10

Hi

Where does that come from?


#11

When I track the error it opens “ListPool.cs”


#12

I mean the full stack trace.


#13

Sorry, I am not sure what you mean :sweat_smile: Still a novice…

Most likely to do with someting I did, so I will try to figure it out.

I got a second error after that with “You are releasing a path which is not claimed at all (most likely it has been pooled already).” So I think somting is going wrong with assigning the paths.


#14

In the console you can see the full stack trace of which code called those methods. They usually help you debug it.


#15

Ah you mean the whole error in the console

InvalidOperationException: You are trying to pool a list twice. Please make sure that you only pool it once.
Pathfinding.Util.ListPool`1[T].Release (System.Collections.Generic.List`1[T] list) (at Assets/AstarPathfindingProject/Core/Misc/ListPool.cs:173)
Pathfinding.MultiTargetPath.OnEnterPool () (at Assets/AstarPathfindingProject/Pathfinders/MultiTargetPath.cs:147)
Pathfinding.Path.Pathfinding.IPathInternals.OnEnterPool () (at Assets/AstarPathfindingProject/Core/Path.cs:825)
Pathfinding.PathPool.Pool (Pathfinding.Path path) (at Assets/AstarPathfindingProject/Core/Misc/PathPool.cs:29)
Pathfinding.Path.Release (System.Object o, System.Boolean silent) (at Assets/AstarPathfindingProject/Core/Path.cs:646)
Pathfinding.AIPath.OnPathComplete (Pathfinding.Path newPath) (at Assets/AstarPathfindingProject/Core/AI/AIPath.cs:285)
Pathfinding.AIBase.SetPath (Pathfinding.Path path) (at Assets/AstarPathfindingProject/Core/AI/AIBase.cs:472)
TaskManager.WorkerScript.MoveTo (System.Collections.Generic.List`1[T] position, System.Action onArrivedAtPosition) (at Assets/Scripts/Units/Worker/WorkerScript.cs:74)
TaskManager.WorkerTaskAI.ExecuteTask_DigTile (TaskManager.TaskManager+Task+DigTile digTileTask) (at Assets/Scripts/Units/Worker/WorkerTaskAI.cs:94)
TaskManager.WorkerTaskAI.RequestNextTask () (at Assets/Scripts/Units/Worker/WorkerTaskAI.cs:74)
TaskManager.WorkerTaskAI.Update () (at Assets/Scripts/Units/Worker/WorkerTaskAI.cs:44)

It is a bit random, sometimes i works multiple times, sometimes just once.