Class AStarGrid2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
AStarGrid2D is a variant of AStar2D that is specialized for partial 2D grids. It is simpler to use because it doesn't require you to manually create points and connect them together. This class also supports multiple types of heuristics, modes for diagonal movement, and a jumping mode to speed up calculations.
To use AStarGrid2D, you only need to set the Region of the grid, optionally set the CellSize, and then call the Update() method:
AStarGrid2D astarGrid = new AStarGrid2D();
astarGrid.Region = new Rect2I(0, 0, 32, 32);
astarGrid.CellSize = new Vector2I(16, 16);
astarGrid.Update();
GD.Print(astarGrid.GetIdPath(Vector2I.Zero, new Vector2I(3, 4))); // prints (0, 0), (1, 1), (2, 2), (3, 3), (3, 4)
GD.Print(astarGrid.GetPointPath(Vector2I.Zero, new Vector2I(3, 4))); // prints (0, 0), (16, 16), (32, 32), (48, 48), (48, 64)
To remove a point from the pathfinding grid, it must be set as "solid" with SetPointSolid(Vector2I, bool).
public class AStarGrid2D : RefCounted, IDisposable
- Inheritance
-
AStarGrid2D
- Implements
- Inherited Members
Constructors
AStarGrid2D()
public AStarGrid2D()
Properties
CellSize
The size of the point cell which will be applied to calculate the resulting point position returned by GetPointPath(Vector2I, Vector2I). If changed, Update() needs to be called before finding the next path.
public Vector2 CellSize { get; set; }
Property Value
DefaultComputeHeuristic
The default AStarGrid2D.Heuristic which will be used to calculate the cost between two points if _ComputeCost(Vector2I, Vector2I) was not overridden.
public AStarGrid2D.Heuristic DefaultComputeHeuristic { get; set; }
Property Value
DefaultEstimateHeuristic
The default AStarGrid2D.Heuristic which will be used to calculate the cost between the point and the end point if _EstimateCost(Vector2I, Vector2I) was not overridden.
public AStarGrid2D.Heuristic DefaultEstimateHeuristic { get; set; }
Property Value
DiagonalMode
A specific AStarGrid2D.DiagonalModeEnum mode which will force the path to avoid or accept the specified diagonals.
public AStarGrid2D.DiagonalModeEnum DiagonalMode { get; set; }
Property Value
JumpingEnabled
Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
Note: Currently, toggling it on disables the consideration of weight scaling in pathfinding.
public bool JumpingEnabled { get; set; }
Property Value
Offset
The offset of the grid which will be applied to calculate the resulting point position returned by GetPointPath(Vector2I, Vector2I). If changed, Update() needs to be called before finding the next path.
public Vector2 Offset { get; set; }
Property Value
Region
The region of grid cells available for pathfinding. If changed, Update() needs to be called before finding the next path.
public Rect2I Region { get; set; }
Property Value
Size
The size of the grid (number of cells of size CellSize on each axis). If changed, Update() needs to be called before finding the next path.
Deprecated. Use Region instead.
[Obsolete("This property is deprecated.")]
public Vector2I Size { get; set; }
Property Value
Methods
Clear()
Clears the grid and sets the Region to Rect2i(0, 0, 0, 0)
.
public void Clear()
FillSolidRegion(Rect2I, bool)
Fills the given region
on the grid with the specified value for the solid flag.
Note: Calling Update() is not needed after the call of this function.
public void FillSolidRegion(Rect2I region, bool solid = true)
Parameters
FillWeightScaleRegion(Rect2I, float)
Fills the given region
on the grid with the specified value for the weight scale.
Note: Calling Update() is not needed after the call of this function.
public void FillWeightScaleRegion(Rect2I region, float weightScale)
Parameters
GetIdPath(Vector2I, Vector2I)
Returns an array with the IDs of the points that form the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path.
public Array<Vector2I> GetIdPath(Vector2I fromId, Vector2I toId)
Parameters
Returns
GetPointPath(Vector2I, Vector2I)
Returns an array with the points that are in the path found by AStarGrid2D between the given points. The array is ordered from the starting point to the ending point of the path.
Note: This method is not thread-safe. If called from a GodotThread, it will return an empty Vector2[] and will print an error message.
public Vector2[] GetPointPath(Vector2I fromId, Vector2I toId)
Parameters
Returns
- Vector2[]
GetPointPosition(Vector2I)
Returns the position of the point associated with the given id
.
public Vector2 GetPointPosition(Vector2I id)
Parameters
id
Vector2I
Returns
GetPointWeightScale(Vector2I)
Returns the weight scale of the point associated with the given id
.
public float GetPointWeightScale(Vector2I id)
Parameters
id
Vector2I
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsDirty()
Indicates that the grid parameters were changed and Update() needs to be called.
public bool IsDirty()
Returns
IsInBounds(int, int)
Returns true
if the x
and y
is a valid grid coordinate (id), i.e. if it is inside Region. Equivalent to region.has_point(Vector2i(x, y))
.
public bool IsInBounds(int x, int y)
Parameters
Returns
IsInBoundsv(Vector2I)
Returns true
if the id
vector is a valid grid coordinate, i.e. if it is inside Region. Equivalent to region.has_point(id)
.
public bool IsInBoundsv(Vector2I id)
Parameters
id
Vector2I
Returns
IsPointSolid(Vector2I)
Returns true
if a point is disabled for pathfinding. By default, all points are enabled.
public bool IsPointSolid(Vector2I id)
Parameters
id
Vector2I
Returns
SetPointSolid(Vector2I, bool)
Disables or enables the specified point for pathfinding. Useful for making an obstacle. By default, all points are enabled.
Note: Calling Update() is not needed after the call of this function.
public void SetPointSolid(Vector2I id, bool solid = true)
Parameters
SetPointWeightScale(Vector2I, float)
Sets the weightScale
for the point with the given id
. The weightScale
is multiplied by the result of _ComputeCost(Vector2I, Vector2I) when determining the overall cost of traveling across a segment from a neighboring point to this point.
Note: Calling Update() is not needed after the call of this function.
public void SetPointWeightScale(Vector2I id, float weightScale)
Parameters
Update()
Updates the internal state of the grid according to the parameters to prepare it to search the path. Needs to be called if parameters like Region, CellSize or Offset are changed. IsDirty() will return true
if this is the case and this needs to be called.
Note: All point data (solidity and weight scale) will be cleared.
public void Update()
_ComputeCost(Vector2I, Vector2I)
Called when computing the cost between two connected points.
Note that this function is hidden in the default AStarGrid2D class.
public virtual float _ComputeCost(Vector2I fromId, Vector2I toId)
Parameters
Returns
_EstimateCost(Vector2I, Vector2I)
Called when estimating the cost between a point and the path's ending point.
Note that this function is hidden in the default AStarGrid2D class.
public virtual float _EstimateCost(Vector2I fromId, Vector2I toId)