Custom class not working with other scripts

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
	

Hi!

How do you access this script?

Have you tried instantiating it as a class?


var pf : Pathfinding = null

func _ready() -> void:
	var pf : Pathfinding = Pathfinding.new()

PS: sorry, Pathfinding should not be capitalized, because you are not capitalizing it after class_name either.

I think you need it to extend something. E.g. “extends node” at the top if you dont need stuff from some other class. Could be wrong though.

The extends keyword is used together with Nodes, but class_name is used with classes.

Which one you use, depends on what you want. As far as I can tell from your code, these functions don’t depend on any specific Node, but they are just fine being in a script only.

You can try to access the script from another via a class (as shown in my above code snippet).

PS: @baba and @rowanmulhollem, sorry I thought I was addressing rowan.

Please explain what you mean by “wont work” give exact error messages, explain what you expected to happen versus what really happens.

Never mind I figured it out. I just had to create an instance of the class in the script using a variable. Thanks anyway!

1 Like