A* Pathfinding Project  3.8.12
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
Seeker Class Reference

Handles path calls for a single unit. More...

Detailed Description

Handles path calls for a single unit.

This is a component which is meant to be attached to a single unit (AI, Robot, Player, whatever) to handle it's pathfinding calls. It also handles post-processing of paths using modifiers.

See Also
Searching for paths
Examples:
AstarAI.cs, EndingCondition.cs, and MultiTargetPathExample.cs.

Public Types

enum  ModifierPass { PreProcess, PostProcess = 2 }
 

Public Member Functions

 Seeker ()
 
void CancelCurrentPathRequest (bool pool=true)
 Stop calculating the current path request.
 
void DeregisterModifier (IPathModifier mod)
 Called by modifiers when they are disabled or destroyed.
 
Path GetCurrentPath ()
 Path that is currently being calculated or was last calculated.
 
ABPath GetNewPath (Vector3 start, Vector3 end)
 Returns a new path instance.
 
bool IsDone ()
 Is the current path done calculating.
 
void OnDestroy ()
 Cleans up some variables.
 
void OnDrawGizmos ()
 Draws gizmos for the Seeker.
 
void PostProcess (Path p)
 Post Processes the path.
 
void RegisterModifier (IPathModifier mod)
 Called by modifiers to register themselves.
 
void ReleaseClaimedPath ()
 Releases the path used for gizmos (if any).
 
void RunModifiers (ModifierPass pass, Path p)
 Runs modifiers on path p.
 
MultiTargetPath StartMultiTargetPath (Vector3 start, Vector3[] endPoints, bool pathsForAll, OnPathDelegate callback=null, int graphMask=-1)
 Starts a Multi Target Path from one start point to multiple end points.
 
MultiTargetPath StartMultiTargetPath (Vector3[] startPoints, Vector3 end, bool pathsForAll, OnPathDelegate callback=null, int graphMask=-1)
 Starts a Multi Target Path from multiple start points to a single target point.
 
MultiTargetPath StartMultiTargetPath (MultiTargetPath p, OnPathDelegate callback=null, int graphMask=-1)
 Starts a Multi Target Path.
 
Path StartPath (Vector3 start, Vector3 end)
 Call this function to start calculating a path.
 
Path StartPath (Vector3 start, Vector3 end, OnPathDelegate callback)
 Call this function to start calculating a path.
 
Path StartPath (Vector3 start, Vector3 end, OnPathDelegate callback, int graphMask)
 Call this function to start calculating a path.
 
Path StartPath (Path p, OnPathDelegate callback=null, int graphMask=-1)
 Call this function to start calculating a path.
 

Public Attributes

bool detailedGizmos
 Enables drawing of the non-postprocessed path using Gizmos.
 
bool drawGizmos = true
 Enables drawing of the last calculated path using Gizmos.
 
OnPathDelegate pathCallback
 Callback for when a path is completed.
 
OnPathDelegate postProcessPath
 Called after a path has been calculated, right before modifiers are executed.
 
OnPathDelegate preProcessPath
 Called before pathfinding is started.
 
StartEndModifier startEndModifier = new StartEndModifier()
 Path modifier which tweaks the start and end points of a path.
 
int[] tagPenalties = new int[32]
 Penalties for each tag.
 
int traversableTags = -1
 The tags which the Seeker can traverse.
 

Protected Attributes

uint lastPathID
 The path ID of the last path queried.
 
Path path
 The current path.
 
TagMask traversableTagsCompatibility = new TagMask(-1, -1)
 Required for serialization backwards compatibility.
 

Private Member Functions

void Awake ()
 Initializes a few variables.
 
void
ISerializationCallbackReceiver. 
OnAfterDeserialize ()
 Handle serialization backwards compatibility.
 
void
ISerializationCallbackReceiver. 
OnBeforeSerialize ()
 Handle serialization backwards compatibility.
 
void OnMultiPathComplete (Path p)
 Called once for a MultiTargetPath.
 
void OnPartialPathComplete (Path p)
 Called for each path in a MultiTargetPath.
 
void OnPathComplete (Path p)
 Called when a path has completed.
 
void OnPathComplete (Path p, bool runModifiers, bool sendCallbacks)
 Called when a path has completed.
 
void StartPathInternal (Path p, OnPathDelegate callback)
 Internal method to start a path and mark it as the currently active path.
 

Private Attributes

List< GraphNodelastCompletedNodePath
 Used for drawing gizmos.
 
List< Vector3 > lastCompletedVectorPath
 Used for drawing gizmos.
 
readonly List< IPathModifiermodifiers = new List<IPathModifier>()
 Internal list of all modifiers.
 
readonly OnPathDelegate onPartialPathDelegate
 Cached delegate to avoid allocating one every time a path is started.
 
readonly OnPathDelegate onPathDelegate
 Cached delegate to avoid allocating one every time a path is started.
 
Path prevPath
 Previous path.
 
OnPathDelegate tmpPathCallback
 Temporary callback only called for the current path.
 

Member Enumeration Documentation

Enumerator:
PreProcess 
PostProcess 

Constructor & Destructor Documentation

Seeker ( )

Member Function Documentation

void Awake ( )
private

Initializes a few variables.

void CancelCurrentPathRequest ( bool  pool = true)

Stop calculating the current path request.

If this Seeker is currently calculating a path it will be canceled. The callback (usually to a method named OnPathComplete) will soon be called with a path that has the 'error' field set to true.

This does not stop the character from moving, it just aborts the path calculation.

Parameters
poolIf true then the path will be pooled when the pathfinding system is done with it.
void DeregisterModifier ( IPathModifier  mod)

Called by modifiers when they are disabled or destroyed.

Path GetCurrentPath ( )

Path that is currently being calculated or was last calculated.

You should rarely have to use this. Instead get the path when the path callback is called.

See Also
pathCallback
ABPath GetNewPath ( Vector3  start,
Vector3  end 
)

Returns a new path instance.

The path will be taken from the path pool if path recycling is turned on.
This path can be sent to StartPath(Path,OnPathDelegate,int) with no change, but if no change is required StartPath(Vector3,Vector3,OnPathDelegate) does just that.

var seeker = GetComponent<Seeker>();
Path p = seeker.GetNewPath (transform.position, transform.position+transform.forward*100);
// Disable heuristics on just this path for example
p.heuristic = Heuristic.None;
seeker.StartPath (p, OnPathComplete);
bool IsDone ( )

Is the current path done calculating.

Returns true if the current path has been returned or if the path is null.

Note
Do not confuse this with Pathfinding.Path.IsDone. They usually return the same value, but not always since the path might be completely calculated, but it has not yet been processed by the Seeker.
Since
Added in 3.0.8
Version
Behaviour changed in 3.2
void ISerializationCallbackReceiver. OnAfterDeserialize ( )
private

Handle serialization backwards compatibility.

void ISerializationCallbackReceiver. OnBeforeSerialize ( )
private

Handle serialization backwards compatibility.

void OnDestroy ( )

Cleans up some variables.

Releases any eventually claimed paths. Calls OnDestroy on the startEndModifier.

See Also
ReleaseClaimedPath
startEndModifier
void OnDrawGizmos ( )

Draws gizmos for the Seeker.

void OnMultiPathComplete ( Path  p)
private

Called once for a MultiTargetPath.

Only returns the path, does not post process.

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here
void OnPartialPathComplete ( Path  p)
private

Called for each path in a MultiTargetPath.

Only post processes the path, does not return it.

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here
void OnPathComplete ( Path  p)
private

Called when a path has completed.

This should have been implemented as optional parameter values, but that didn't seem to work very well with delegates (the values weren't the default ones)

See Also
OnPathComplete(Path,bool,bool)
void OnPathComplete ( Path  p,
bool  runModifiers,
bool  sendCallbacks 
)
private

Called when a path has completed.

Will post process it and return it by calling tmpPathCallback and pathCallback

void PostProcess ( Path  p)

Post Processes the path.

This will run any modifiers attached to this GameObject on the path. This is identical to calling RunModifiers(ModifierPass.PostProcess, path)

See Also
RunModifiers
Since
Added in 3.2
void RegisterModifier ( IPathModifier  mod)

Called by modifiers to register themselves.

void ReleaseClaimedPath ( )

Releases the path used for gizmos (if any).

The seeker keeps the latest path claimed so it can draw gizmos. In some cases this might not be desireable and you want it released. In that case, you can call this method to release it (not that path gizmos will then not be drawn).

If you didn't understand anything from the description above, you probably don't need to use this method.

See Also
Pooling
void RunModifiers ( ModifierPass  pass,
Path  p 
)

Runs modifiers on path p.

MultiTargetPath StartMultiTargetPath ( Vector3  start,
Vector3[]  endPoints,
bool  pathsForAll,
OnPathDelegate  callback = null,
int  graphMask = -1 
)

Starts a Multi Target Path from one start point to multiple end points.

A Multi Target Path will search for all the end points in one search and will return all paths if pathsForAll is true, or only the shortest one if pathsForAll is false.

Parameters
startThe start point of the path
endPointsThe end points of the path
pathsForAllIndicates whether or not a path to all end points should be searched for or only to the closest one
callbackThe function to call when the path has been calculated
graphMaskMask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.

callback and pathCallback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here
See Also
Pathfinding.MultiTargetPath
Example of how to use multi-target-paths
MultiTargetPath StartMultiTargetPath ( Vector3[]  startPoints,
Vector3  end,
bool  pathsForAll,
OnPathDelegate  callback = null,
int  graphMask = -1 
)

Starts a Multi Target Path from multiple start points to a single target point.

A Multi Target Path will search from all start points to the target point in one search and will return all paths if pathsForAll is true, or only the shortest one if pathsForAll is false.

Parameters
startPointsThe start points of the path
endThe end point of the path
pathsForAllIndicates whether or not a path from all start points should be searched for or only to the closest one
callbackThe function to call when the path has been calculated
graphMaskMask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.

callback and pathCallback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here
See Also
Pathfinding.MultiTargetPath
Example of how to use multi-target-paths
MultiTargetPath StartMultiTargetPath ( MultiTargetPath  p,
OnPathDelegate  callback = null,
int  graphMask = -1 
)

Starts a Multi Target Path.

Takes a MultiTargetPath and wires everything up for it to send callbacks to the seeker for post-processing.

Parameters
pThe path to start calculating
callbackThe function to call when the path has been calculated
graphMaskMask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.

callback and pathCallback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)

A* Pro Feature:
This is an A* Pathfinding Project Pro feature only. This function/class/variable might not exist in the Free version of the A* Pathfinding Project or the functionality might be limited
The Pro version can be bought here
See Also
Pathfinding.MultiTargetPath
Example of how to use multi-target-paths
Version
Since 3.8.3 calling this method behaves identically to calling StartPath with a MultiTargetPath.
Since 3.8.3 this method also sets enabledTags and tagPenalties on the path object.
Path StartPath ( Vector3  start,
Vector3  end 
)

Call this function to start calculating a path.

Parameters
startThe start point of the path
endThe end point of the path
Examples:
AstarAI.cs, and EndingCondition.cs.
Path StartPath ( Vector3  start,
Vector3  end,
OnPathDelegate  callback 
)

Call this function to start calculating a path.

Parameters
startThe start point of the path
endThe end point of the path
callbackThe function to call when the path has been calculated

callback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)

Path StartPath ( Vector3  start,
Vector3  end,
OnPathDelegate  callback,
int  graphMask 
)

Call this function to start calculating a path.

Parameters
startThe start point of the path
endThe end point of the path
callbackThe function to call when the path has been calculated
graphMaskMask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.

callback will be called when the path has completed. Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)

Path StartPath ( Path  p,
OnPathDelegate  callback = null,
int  graphMask = -1 
)

Call this function to start calculating a path.

Parameters
pThe path to start calculating
callbackThe function to call when the path has been calculated
graphMaskMask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.

The callback will be called when the path has been calculated (which may be several frames into the future). The callback will not be called if a new path request is started before this path request has been calculated.

Version
Since 3.8.3 this method works properly if a MultiTargetPath is used. It now behaves identically to the StartMultiTargetPath(MultiTargetPath) method.
void StartPathInternal ( Path  p,
OnPathDelegate  callback 
)
private

Internal method to start a path and mark it as the currently active path.

Member Data Documentation

bool detailedGizmos

Enables drawing of the non-postprocessed path using Gizmos.

The path will show up in orange.

Requires that drawGizmos is true.

This will show the path before any post processing such as smoothing is applied.

See Also
drawGizmos
OnDrawGizmos
bool drawGizmos = true

Enables drawing of the last calculated path using Gizmos.

The path will show up in green.

See Also
OnDrawGizmos
List<GraphNode> lastCompletedNodePath
private

Used for drawing gizmos.

List<Vector3> lastCompletedVectorPath
private

Used for drawing gizmos.

uint lastPathID
protected

The path ID of the last path queried.

readonly List<IPathModifier> modifiers = new List<IPathModifier>()
private

Internal list of all modifiers.

readonly OnPathDelegate onPartialPathDelegate
private

Cached delegate to avoid allocating one every time a path is started.

readonly OnPathDelegate onPathDelegate
private

Cached delegate to avoid allocating one every time a path is started.

Path path
protected

The current path.

OnPathDelegate pathCallback

Callback for when a path is completed.

Movement scripts should register to this delegate.
A temporary callback can also be set when calling StartPath, but that delegate will only be called for that path

Examples:
EndingCondition.cs, and MultiTargetPathExample.cs.
OnPathDelegate postProcessPath

Called after a path has been calculated, right before modifiers are executed.

Can be anything which only modifies the positions (Vector3[]).

OnPathDelegate preProcessPath

Called before pathfinding is started.

Path prevPath
private

Previous path.

Used to draw gizmos

StartEndModifier startEndModifier = new StartEndModifier()

Path modifier which tweaks the start and end points of a path.

int [] tagPenalties = new int[32]

Penalties for each tag.

Tag 0 which is the default tag, will have added a penalty of tagPenalties[0]. These should only be positive values since the A* algorithm cannot handle negative penalties.

Note
This array should always have a length of 32 otherwise the system will ignore it.
See Also
Pathfinding.Path.tagPenalties
OnPathDelegate tmpPathCallback
private

Temporary callback only called for the current path.

This value is set by the StartPath functions

int traversableTags = -1

The tags which the Seeker can traverse.

Note
This field is a bitmask.
See Also
https://en.wikipedia.org/wiki/Mask_(computing)
TagMask traversableTagsCompatibility = new TagMask(-1, -1)
protected

Required for serialization backwards compatibility.

Since
3.6.8

The documentation for this class was generated from the following file: