Godot Version
4.4
Issue
I followed a tutorial on YouTube by a wonderful guy named Mostly Mad Productions. He made a tutorial on Grid-based 2D pathfinding and it worked for him.
I followed every single line of code within it, I understand it a little except for the error:
grid_based_ai.gd:30 @ _move_ai(): Can't get id path. Point (56, 17) out of bounds
It’s very strange. I don’t think it’s outta bounds.
Code I followed from tutorial!
extends CharacterBody2D
const TILE_SIZE = 16
const TURNS_TO_MOVE: int = 2
@export var tilemap_layer_node: TileMapLayer = null
@export var player_node: CharacterBody2D = null
@export var visual_path_line2D: Line2D = null
var pathfiding_grid: AStarGrid2D = AStarGrid2D.new()
var path_to_player: Array = []
var turn_counter: int = 1
func _ready() -> void:
visual_path_line2D.global_position = Vector2(TILE_SIZE/2.0, TILE_SIZE/2.0)
player_node.player_did_a_move.connect(_move_ai)
pathfiding_grid.region = tilemap_layer_node.get_used_rect()
pathfiding_grid.cell_size = Vector2(TILE_SIZE, TILE_SIZE)
pathfiding_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
pathfiding_grid.update()
for cell in tilemap_layer_node.get_used_cells():
pathfiding_grid.set_point_solid(cell, true)
_move_ai()
func _move_ai():
path_to_player = pathfiding_grid.get_point_path(global_position / TILE_SIZE, player_node.global_position / TILE_SIZE)
visual_path_line2D.points = path_to_player
if turn_counter != TURNS_TO_MOVE:
turn_counter += 1
else:
if path_to_player.size() > 1:
path_to_player.remove_at(0)
var go_to_pos: Vector2 = path_to_player[0] + Vector2(TILE_SIZE/2.0, TILE_SIZE/2.0)
if go_to_pos.x != global_position.x:
$Sprite2D.flip_h = false if go_to_pos.x > global_position.x else true
$Sprite2D/shadow.flip_h = $Sprite2D.flip_h
global_position = go_to_pos
visual_path_line2D.points = path_to_player
turn_counter = 1
I re-watched the video a dozen times and found nothing. The two scripts look identical.
It’s something wrong with these lines:
var pathfiding_grid: AStarGrid2D = AStarGrid2D.new()
path_to_player = pathfiding_grid.get_point_path(global_position / TILE_SIZE, player_node.global_position / TILE_SIZE)
I also just added this to _ready():
print(tilemap_layer_node.get_used_rect())
I got this:
[P: (-4, -3), S: (30, 18)]
Weird, I just noticed that every time I move, it has the error I provided, then the point of where I’m at, and the little line I just provided.
I’d also like to add, the enemy this is attached to also follows the player when the Player is like right on the Enemy.