Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
update code for godot 4.XxccdsIn godot 4.X, you cannot attach astargrid.gd to a empty tilemap node. so I attach it to the TileMap in Level scene. and I use a layer in tilemap to store path cell. so there are some code needed to update like below.
astargrid.gd
```gdscript
extends TileMap
var _astar := AStar2D.new()
@export var start_point := Vector2.ZERO
@export var goal_point := Vector2.ZERO
var walkable_cells: PackedVector2Array
var _start_id := 0
var _goal_id := 0
const NEIGHBOR_DIRECTIONS := [
Vector2.UP,
Vector2.LEFT,
Vector2.RIGHT,
Vector2.DOWN
]
func _create_astar_points():
var cell_id := 0
for cell in walkable_cells:
_astar.add_point(cell_id, cell)
if Vector2i(cell) == local_to_map(start_point):
_start_id = cell_id
if Vector2i(cell) == local_to_map(goal_point):
_goal_id = cell_id
cell_id += 1
func _connect_neighbor_cells():
# Turn the walkable_cells into an Array to find its elements indices
var walkable_cells_array := Array(walkable_cells)
# Iterate on each element of the get_points() list then look for neighbor
# cells
# offsetting the current cell using the directions in the
# NEIGHBOR_DIRECTIONS list
for point in _astar.get_point_ids():
# Figure out the current cell ID
var cell = _astar.get_point_position(point)
# Offset the current cell by one cell at each direction provided in the
# NEIGHBOR_DIRECTIONS
for direction in NEIGHBOR_DIRECTIONS:
var neighbor_cell_id = walkable_cells_array.find(cell + direction)
# Skip cells that weren't found
if neighbor_cell_id == -1:
continue
# Connect the current cell to the current neighbor cell
_astar.connect_points(point, neighbor_cell_id)
func _get_astar_path() -> PackedVector2Array:
var astar_path: PackedVector2Array
_create_astar_points()
_connect_neighbor_cells()
# Creates a walkable path from the start_point to the goal_point
astar_path = _astar.get_point_path(_start_id, _goal_id)
return astar_path
func get_walkable_path() -> PackedVector2Array:
var walkable_path := PackedVector2Array()
var astar_path = _get_astar_path()
# Convert the map cells in the `astar_path` to world positions
for cell in astar_path:
var point := map_to_local(cell)
# Offset the positions to get the center of each cell instead of its top
# left corner
walkable_path.append(to_global(point))
return walkable_path
```
level.gd
```gdscript
@onready var path_preview = $PathPreview
@onready var tile_map = $TileMap
@onready var start = $start
@onready var goal = $goal
func _ready() -> void:
path_preview.clear_points()
tile_map.start_point = start.global_position
tile_map.goal_point = goal.global_position
tile_map.walkable_cells = tile_map.get_used_cells(1)
path_preview.points = tile_map.get_walkable_path()
```
one more thing, there is a new class named AStarGrid2D in Godot 4.X. This class maybe more easy to use with tilemap than AStar2D?10Aug. 26, 2024
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.