Inheritance #

RefCounted

- AESContext
- AStar2D
- AStar3D
- AStarGrid2D
- AudioEffectInstance (1)
- AudioSample
- AudioSamplePlayback
- AudioStreamPlayback (5)
- CameraFeed
- CharFXTransform
- ConfigFile
- Crypto
- DTLSServer
- DirAccess
- ENetConnection
- EditorContextMenuPlugin
- EditorDebuggerPlugin
- EditorDebuggerSession
- EditorExportPlatform (6)
- EditorExportPlugin
- EditorExportPreset
- EditorFeatureProfile
- EditorFileSystemImportFormatSupportQuery
- EditorInspectorPlugin
- EditorResourceConversionPlugin
- EditorResourcePreviewGenerator
- EditorResourceTooltipPlugin
- EditorSceneFormatImporter (4)
- EditorScenePostImport
- EditorScenePostImportPlugin
- EditorScript
- EditorTranslationParserPlugin
- EncodedObjectAsID
- EngineProfiler
- Expression
- FileAccess
- GLTFObjectModelProperty
- HMACContext
- HTTPClient
- HashingContext
- ImageFormatLoader (1)
- JavaClass
- JavaObject
- JavaScriptObject
- KinematicCollision2D
- KinematicCollision3D
- Lightmapper (1)
- MeshConvexDecompositionSettings
- MeshDataTool
- MultiplayerAPI (2)
- Mutex
- NavigationPathQueryParameters2D
- NavigationPathQueryParameters3D
- NavigationPathQueryResult2D
- NavigationPathQueryResult3D
- Node3DGizmo (1)
- OggPacketSequencePlayback
- OpenXRAPIExtension
- PCKPacker
- PackedDataContainerRef
- PacketPeer (8)
- PhysicsPointQueryParameters2D
- PhysicsPointQueryParameters3D
- PhysicsRayQueryParameters2D
- PhysicsRayQueryParameters3D
- PhysicsShapeQueryParameters2D
- PhysicsShapeQueryParameters3D
- PhysicsTestMotionParameters2D
- PhysicsTestMotionParameters3D
- PhysicsTestMotionResult2D
- PhysicsTestMotionResult3D
- RDAttachmentFormat
- RDFramebufferPass
- RDPipelineColorBlendState
- RDPipelineColorBlendStateAttachment
- RDPipelineDepthStencilState
- RDPipelineMultisampleState
- RDPipelineRasterizationState
- RDPipelineSpecializationConstant
- RDSamplerState
- RDShaderSource
- RDTextureFormat
- RDTextureView
- RDUniform
- RDVertexAttribute
- RandomNumberGenerator
- RegEx
- RegExMatch
- RenderSceneBuffers (2)
- RenderSceneBuffersConfiguration
- Resource (103)
- ResourceFormatLoader
- ResourceFormatSaver
- ResourceImporter (16)
- SceneState
- SceneTreeTimer
- Semaphore
- SkinReference
- StreamPeer (5)
- SurfaceTool
- TCPServer
- TLSOptions
- TextLine
- TextParagraph
- TextServer (1)
- Thread
- TranslationDomain
- TriangleMesh
- Tween
- Tweener (5)
- UDPServer
- UPNP
- UPNPDevice
- WeakRef
- WebRTCPeerConnection (1)
- XMLParser
- XRInterface (4)
- XRPose
- XRTracker (2)
- ZIPPacker
- ZIPReader
AStarGrid2D

Table of contents

AStarGrid2D #

is_refcounted, is_instantiable, core, not_builtin_classes

An implementation of A* for finding the shortest path between two points on a partial 2D grid.

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 cell_size, and then call the update method:

GDScript

var astar_grid = AStarGrid2D.new()
astar_grid.region = Rect2i(0, 0, 32, 32)
astar_grid.cell_size = Vector2(16, 16)
astar_grid.update()
print(astar_grid.get_id_path(Vector2i(0, 0), Vector2i(3, 4))) # Prints [(0, 0), (1, 1), (2, 2), (3, 3), (3, 4)]
print(astar_grid.get_point_path(Vector2i(0, 0), Vector2i(3, 4))) # Prints [(0, 0), (16, 16), (32, 32), (48, 48), (48, 64)]

C#

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 set_point_solid.

Members #

var cell_shape = CELL_SHAPE_SQUARE#

The cell shape. Affects how the positions are placed in the grid. If changed, update needs to be called before finding the next path.

var cell_size: Vector2 = Vector2(1, 1)#

The size of the point cell which will be applied to calculate the resulting point position returned by get_point_path. If changed, update needs to be called before finding the next path.

var default_compute_heuristic = HEURISTIC_EUCLIDEAN#

The default Heuristic which will be used to calculate the cost between two points if _compute_cost was not overridden.

var default_estimate_heuristic = HEURISTIC_EUCLIDEAN#

The default Heuristic which will be used to calculate the cost between the point and the end point if _estimate_cost was not overridden.

var diagonal_mode = DIAGONAL_MODE_ALWAYS#

A specific DiagonalMode mode which will force the path to avoid or accept the specified diagonals.

var jumping_enabled: bool = false#

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.

var offset: Vector2 = Vector2(0, 0)#

The offset of the grid which will be applied to calculate the resulting point position returned by get_point_path. If changed, update needs to be called before finding the next path.

var region: Rect2i = Rect2i(0, 0, 0, 0)#

The region of grid cells available for pathfinding. If changed, update needs to be called before finding the next path.

var size: Vector2i = Vector2i(0, 0)#

The size of the grid (number of cells of size cell_size on each axis). If changed, update needs to be called before finding the next path.

Methods #

virtual const func _compute_cost(to_id: Vector2i) -> float#

Called when computing the cost between two connected points.

Note that this function is hidden in the default AStarGrid2D class.

virtual const func _estimate_cost(end_id: Vector2i) -> float#

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.

func clear() -> void#

Clears the grid and sets the region to Rect2i(0, 0, 0, 0).

func fill_solid_region(solid: bool = true) -> void#

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.

func fill_weight_scale_region(weight_scale: float) -> void#

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.

func get_id_path(allow_partial_path: bool = false) -> 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.

If there is no valid path to the target, and allow_partial_path is true, returns a path to the point closest to the target that can be reached.

Note: When allow_partial_path is true and to_id is solid the search may take an unusually long time to finish.

const func get_point_data_in_region(region: Rect2i) -> Dictionary[]#

Returns an array of dictionaries with point data (id: Vector2i, position: Vector2, solid: bool, weight_scale: float) within a region.

func get_point_path(allow_partial_path: bool = false) -> PackedVector2Array#

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.

If there is no valid path to the target, and allow_partial_path is true, returns a path to the point closest to the target that can be reached.

Note: This method is not thread-safe. If called from a Thread, it will return an empty array and will print an error message.

Additionally, when allow_partial_path is true and to_id is solid the search may take an unusually long time to finish.

const func get_point_position(id: Vector2i) -> Vector2#

Returns the position of the point associated with the given id.

const func get_point_weight_scale(id: Vector2i) -> float#

Returns the weight scale of the point associated with the given id.

const func is_dirty() -> bool#

Indicates that the grid parameters were changed and update needs to be called.

const func is_in_bounds(y: int) -> bool#

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)).

const func is_in_boundsv(id: Vector2i) -> bool#

Returns true if the id vector is a valid grid coordinate, i.e. if it is inside region. Equivalent to region.has_point(id).

const func is_point_solid(id: Vector2i) -> bool#

Returns true if a point is disabled for pathfinding. By default, all points are enabled.

func set_point_solid(solid: bool = true) -> void#

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.

func set_point_weight_scale(weight_scale: float) -> void#

Sets the weight_scale for the point with the given id. The weight_scale is multiplied by the result of _compute_cost 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.

func update() -> void#

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, cell_size or offset are changed. is_dirty 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.

Annotations #

Constants #

const HEURISTIC_EUCLIDEAN = 0 enum Heuristic#

The Euclidean heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = sqrt(dx * dx + dy * dy)

Note: This is also the internal heuristic used in AStar3D and AStar2D by default (with the inclusion of possible z-axis coordinate).

const HEURISTIC_MANHATTAN = 1 enum Heuristic#

The Manhattan heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = dx + dy

Note: This heuristic is intended to be used with 4-side orthogonal movements, provided by setting the diagonal_mode to DIAGONAL_MODE_NEVER.

const HEURISTIC_OCTILE = 2 enum Heuristic#

The Octile heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
f = sqrt(2) - 1
result = (dx < dy) ? f * dx + dy : f * dy + dx;

const HEURISTIC_CHEBYSHEV = 3 enum Heuristic#

The Chebyshev heuristic to be used for the pathfinding using the following formula:

dx = abs(to_id.x - from_id.x)
dy = abs(to_id.y - from_id.y)
result = max(dx, dy)

const HEURISTIC_MAX = 4 enum Heuristic#

Represents the size of the Heuristic enum.

const DIAGONAL_MODE_ALWAYS = 0 enum DiagonalMode#

The pathfinding algorithm will ignore solid neighbors around the target cell and allow passing using diagonals.

const DIAGONAL_MODE_NEVER = 1 enum DiagonalMode#

The pathfinding algorithm will ignore all diagonals and the way will be always orthogonal.

const DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE = 2 enum DiagonalMode#

The pathfinding algorithm will avoid using diagonals if at least two obstacles have been placed around the neighboring cells of the specific path segment.

const DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES = 3 enum DiagonalMode#

The pathfinding algorithm will avoid using diagonals if any obstacle has been placed around the neighboring cells of the specific path segment.

const DIAGONAL_MODE_MAX = 4 enum DiagonalMode#

Represents the size of the DiagonalMode enum.

const CELL_SHAPE_SQUARE = 0 enum CellShape#

Rectangular cell shape.

const CELL_SHAPE_ISOMETRIC_RIGHT = 1 enum CellShape#

Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes up-right, and the vertical one goes down-right.

const CELL_SHAPE_ISOMETRIC_DOWN = 2 enum CellShape#

Diamond cell shape (for isometric look). Cell coordinates layout where the horizontal axis goes down-right, and the vertical one goes down-left.

const CELL_SHAPE_MAX = 3 enum CellShape#

Represents the size of the CellShape enum.

Constructors #

Enums #

Heuristic#

enum Heuristic { HEURISTIC_EUCLIDEAN = 0, HEURISTIC_MANHATTAN = 1, HEURISTIC_OCTILE = 2, HEURISTIC_CHEBYSHEV = 3, HEURISTIC_MAX = 4, }

DiagonalMode#

enum DiagonalMode { DIAGONAL_MODE_ALWAYS = 0, DIAGONAL_MODE_NEVER = 1, DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE = 2, DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES = 3, DIAGONAL_MODE_MAX = 4, }

CellShape#

enum CellShape { CELL_SHAPE_SQUARE = 0, CELL_SHAPE_ISOMETRIC_RIGHT = 1, CELL_SHAPE_ISOMETRIC_DOWN = 2, CELL_SHAPE_MAX = 3, }

Operators #

Signals #

Theme Items #

Tutorials #