Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

AStarGrid2D

Inherits: RefCounted < Object

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

Description

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:

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)

To remove a point from the pathfinding grid, it must be set as "solid" with set_point_solid.

Properties

Vector2

cell_size

Vector2(1, 1)

Heuristic

default_compute_heuristic

0

Heuristic

default_estimate_heuristic

0

DiagonalMode

diagonal_mode

0

bool

jumping_enabled

false

Vector2

offset

Vector2(0, 0)

Rect2i

region

Rect2i(0, 0, 0, 0)

Vector2i

size

Vector2i(0, 0)

Methods

float

_compute_cost ( Vector2i from_id, Vector2i to_id ) virtual const

float

_estimate_cost ( Vector2i from_id, Vector2i to_id ) virtual const

void

clear ( )

void

fill_solid_region ( Rect2i region, bool solid=true )

void

fill_weight_scale_region ( Rect2i region, float weight_scale )