Godot Version
4.5.1
Question
Hi, I’m trying to make a custom class using “class_name” that can be used by other scripts but other scripts wont work with it. Even if I delete all the functions an replace them with a singular variable no other scripts will work with it.
If you share code, please wrap it inside three backticks or replace the code in the next block:
class_name pathfinding
#Seconadary functions
func get_tile_in_direction(nav_tiles, position: Vector2i, direction: Vector2, vision_radius: int):
var surrounding = get_surrunding_tile_positions(nav_tiles, position, vision_radius)
var furthest = get_furthest_tiles(position, vision_radius, surrounding)
var pos_vec = Vector2(position)
var angle_pairs = [] # [ {tile, angle_diff} ]
for tile in furthest:
var angle_diff = abs(direction.angle_to(pos_vec.direction_to(tile)))
angle_pairs.append({ "tile": tile, "angle": angle_diff })
# Find minimum angle
var min_angle = INF
for pair in angle_pairs:
if pair.angle < min_angle:
min_angle = pair.angle
# Collect all tiles with that angle
var candidates = []
for pair in angle_pairs:
if abs(pair.angle - min_angle) <= 0.0001:
candidates.append(pair.tile)
var destination = candidates.pick_random()
#print(destination)
return destination
# Tertiary functions
func get_surrunding_tile_positions(nav_tiles, pos: Vector2i, radius: int,):
# Gets surrounding tile positions
var surrounding_tile_positions = []
for x in range(pos.x - radius, pos.x + radius + 1):
for y in range(pos.y - radius, pos.y + radius + 1):
var cell = Vector2i(x, y)
var cell_data = nav_tiles.get_cell_tile_data(cell)
if cell_data == null:
pass
else:
if cell_data.get_custom_data("navicable"):
if cell == pos:
pass
else:
surrounding_tile_positions.append(cell)
# Debug
#nav_tiles.set_cell(cell, 1)
# Returns array of Vector2i
return surrounding_tile_positions
func get_furthest_tiles(position: Vector2i, vision_radius: int, surrounding_tile_positions: Array):
var furthest_tiles = []
for i in surrounding_tile_positions:
if position.distance_to(i) >= vision_radius:
furthest_tiles.append(i)
return furthest_tiles
func get_angles_to_tiles(position: Vector2i, direction: Vector2, furthest_tiles: Array):
var pos = Vector2(position)
var angles_to_tiles = []
for i in furthest_tiles:
var angle_diff = abs(direction.angle_to(pos.direction_to(i)))
angles_to_tiles.append(angle_diff)
return angles_to_tiles