Godot Version
4.5
Question
Hi, I made some code for an enemy in a top down 2d game like the old Zelda games.
I haven’t actually implemented the actual pathfinding code yet. The code I have here is supposed to get the enemy to pick a tile to pathfind to.
How it’s supposed to work is that the enemy has a predetermined direction it wants to go in and then checks for traversable tiles within a certain radius and then picks one that’s in the direction that’s closest to the direction it wants to head in based on which one has the lowest difference between the angle between the angles of the two vectors.
I think I have the logic mostly figured out but the code is still a bit wonky and I would like help fixing it.
If you share code, please wrap it inside three backticks or replace the code in the next block:
extends CharacterBody2D
@onready var nav_tiles = $"../Tiles/NavTiles"
@onready var debug_tiles = $"../Tiles/DebugTiles"
@onready var bug = $"."
var surrounding_tile_positions = []
#var position = Vector2.ZERO
var direction = Vector2.ZERO
var next_destination = Vector2i.ZERO
# Main functions
func _ready() -> void:
direction = Vector2(-1, 0)
func _physics_process(delta: float) -> void:
var position = nav_tiles.local_to_map(bug.position)
surrounding_tile_positions = get_surrounding_tile_positions(position, 2)
#print(surrounding_tile_positions.size())
if surrounding_tile_positions.size() > 0:
next_destination = get_tile_in_direction(position, direction, surrounding_tile_positions)
print(next_destination)
# Secondary functions
func get_surrounding_tile_positions(position: Vector2, vision_radius: int):
var surrounding_tile_positions = []
# Gets positions of surrounding navicable tiles
for x in range(position.x - vision_radius, position.x + vision_radius + 1):
for y in range(position.y - vision_radius, position.y + vision_radius + 1):
var cell = Vector2(x, y)
# Sorts out navicable tiles
var tile_data = nav_tiles.get_cell_tile_data(cell)
if tile_data == null:
pass
elif tile_data.get_custom_data("navicable"):
surrounding_tile_positions.append(cell)
else:
pass
#print(surrounding_tile_positions.size())
return surrounding_tile_positions
func get_tile_in_direction(position: Vector2, direction: Vector2, surrounding_tile_positions: Array):
# Gets angles to tiles
var angles_to_tiles = []
for i in surrounding_tile_positions:
angles_to_tiles.append(position.angle_to(i))
# Gets angle differences
var angle_differences = []
for i in angles_to_tiles:
angle_differences.append(abs(direction.angle() - i))
# Gets lowest angle difference
var lowest_angle_difference = angles_to_tiles.min()
# Gets corrasponding tiles
var tiles_in_rough_direction = []
for i in surrounding_tile_positions:
if position.angle_to(i) == lowest_angle_difference:
tiles_in_rough_direction.append(i)
# Gets the furthest corrasponding tile
if tiles_in_rough_direction.size() > 1:
var distances_to_tiles = []
for i in tiles_in_rough_direction:
distances_to_tiles.append(position.distance_to(i))
var furthest_tile = distances_to_tiles.max()
for i in tiles_in_rough_direction:
if position.distance_to(i) == furthest_tile:
return Vector2i(i)
else:
return Vector2i(tiles_in_rough_direction[0])



