Trying to use a script. I don't know why it won't register the script

Godot Version

4.3

Question

I have a script, denizenNAVIGATIONandMOVEMENT. it has a class_name DenizenNAVIGATIONandMOVEMENT. I have been able to add other scripts with their appropriate class_name without issue, but this one seems to never be valid, or potentially loaded. I have tried preloading, I have physically put the script on the node, I have used set_script() on the node. I have replaced the entire node, restarted godot and computer many times.

This is where I am trying to get the script functioning:

func init(): 
	print("check script is loaded: ", ClassDB.class_exists("DenizenNAVIGATIONandMOVEMENT"))
	var new_script_attachment_attempt = preload("res://scripts/denizenNAVIGATIONandMOVEMENT.gd")
	print("check script is loaded: ", ClassDB.class_exists("DenizenNAVIGATIONandMOVEMENT"))
	get_child(0).set_script(new_script_attachment_attempt)
	print("child 0 : ", get_child(0))
	var child = get_child(0)
	if child:
		var child_script = child.get_script()
		print("child 0 script: ", child_script)
		if child_script is DenizenNAVIGATIONandMOVEMENT:
			controller = child_script as DenizenNAVIGATIONandMOVEMENT
			print("controller succesfully assigned")
		else:
			print("error: childs script is not of the type DenizenNAVIGATIONandMOVEMENT")
			var script_to_pass:DenizenNAVIGATIONandMOVEMENT = DenizenNAVIGATIONandMOVEMENT.new()
			script_to_pass.script
			child.set_script(script_to_pass)
			if child.has_method("_ready"):
				print("child has _ready")
			child_script = child.get_script()
			print("second run: child 0 script: ", child_script)
			if child_script is DenizenNAVIGATIONandMOVEMENT:
				controller = child_script as DenizenNAVIGATIONandMOVEMENT
				print("second run: controller succesfully assigned")
			else:
				print("second run: error: childs script is not of the type DenizenNAVIGATIONandMOVEMENT")
	else:
		print("ERROR: no child found")
	
	
	print("denizen Is inside tree:", is_inside_tree())
	print("denizen physics processing enabled:", is_physics_processing())

The following is the output:

check script is loaded: false
check script is loaded: false
child 0 : CharacterBody2D:<CharacterBody2D#1554610480409>
child 0 script: <GDScript#-9223372005632375515>
error: childs script is not of the type DenizenNAVIGATIONandMOVEMENT
child has _ready
second run: child 0 script: <GDScript#-9223372005632375515>
second run: error: childs script is not of the type DenizenNAVIGATIONandMOVEMENT
denizen Is inside tree:true
denizen physics processing enabled:true

The following is the script I am having problems with:

extends CharacterBody2D

class_name DenizenNAVIGATIONandMOVEMENT
# enums
# consts
# exports
# public vars
# private vars
var movement_speed: float = 100
var agent:NavigationAgent2D
var custom_path_index:int = 0
var custom_path:PackedVector2Array
var steps_for_custom_path:int = 0:
	set(new_value):
		if new_value > 0:
			steps_for_custom_path = new_value
		else:
			inside_building = false
			steps_for_custom_path = new_value
var next_path_position: Vector2:
	set(new_value):
		if new_value != next_path_position:
			print("next path pos: ", next_path_position)
			next_path_position = new_value
var current_target_position:Vector2
# onready vars
# testing vars
# signals

# flags
var inside_building:bool = false:
	set(new_value):
		if new_value:
			#moving into building
			z_index = Globals.inside_building_z_index
			#print("inside building zindex") # TESTING where z index' will be
		else:
			#moving out of building
			z_index = Globals.outside_building_z_index
			#print("outside building zindex")# TESTING where z index' will be
			
			# used to get off the custom path and back to main navigation path
			set_movement_target(current_target_position)
						
		inside_building = new_value# built-in override methods
func _ready(): pass
func _physics_process(delta):
	if agent != null:
		print("denizen 3")
		if agent.is_navigation_finished():
			print("denizen 1")
			
			return
		print("denizen 2")
			
		if next_path_position.distance_to(self.global_position) <= (Globals.grid_size + Globals.grid_size * .1):
			custom_path_index += 1
			if inside_building:
				steps_for_custom_path -=1
		var current_agent_position: Vector2 = self.global_position
		next_path_position = get_next_path_position()
		var new_velocity: Vector2 = next_path_position - current_agent_position
		new_velocity = new_velocity.normalized()
		new_velocity = new_velocity * movement_speed
		velocity = new_velocity
		print("denizen: velocity should be")
		move_and_slide()

# public methods
func set_movement_target(target:Vector2)->void:
	print("denizen set target to: ", target)
	if agent.target_position != target:
		agent.target_position = target
	# same target, do not update

func init(connect_signals_to:Script, _agent:NavigationAgent2D)->void:
	#print("1")
	#if not is_inside_tree():
	#	call_deferred("init", connect_signals_to, _agent)
	#return
	print("agent is initiated, agent: ", _agent)
	agent = _agent
	#agent.target_reached.connect(connect_signals_to._navigationAgent_target_reached)
	agent.link_reached.connect(_on_navigation_agent_2d_link_reached)
	agent.path_desired_distance = 4.0
	agent.target_desired_distance = 4.0
	agent.velocity = Vector2.ZERO
	#call_deferred("actor_setup")
	print("navigation adn movement Is inside tree:", is_inside_tree())
	print("navigation adn movement Physics processing enabled:", is_physics_processing())	#call_deferred("actor_setup")

	

# private methods
func actor_setup():
	await get_tree().physics_frame
	# FIXME
	# not sure what should be here and what should be in init(). 

func get_next_path_position()->Vector2:
	var index:int = agent.get_current_navigation_path_index()
	var next_position:Vector2
	
	# if we are inside a building we will use the custom path that has added
	# the points of a path.curve that go through the building
	if inside_building:
		
		# FIXME 
		# occasionally the index becomes larger than the custom path for some reason.
		# for that reason I have added this to take them out of a building as it has not 
		# keyed correctly
		if custom_path.size()-1 < index + custom_path_index:
			printerr("denizen pathing error, index larger than path.size() for ", self)
			print("index: ", index, ", custom_path_index: ", custom_path_index, ", total: ", index+custom_path_index, "custom_path.size(): ", custom_path.size())
			inside_building = false
		else:
			next_position = custom_path[index + custom_path_index]
	
	# if we are outside of a building we will use the agent' built in
	# get_next_path_position() fucntion
	else:
		next_position = agent.get_next_path_position()
	#print("next positon is: ", next_path_position) #TESTING
	return next_position
# signal methods
func _on_navigation_agent_2d_link_reached(details):
	# when we enter a link, it is going into a building.
	# *******************************
	# details is a dictionary, and its components are as follows: 
		# position: The start position of the link that was reached.
		# type: Always NavigationPathQueryResult2D.PATH_SEGMENT_TYPE_LINK.
		# rid: The RID of the link.
		# owner: The object which manages the link (usually NavigationLink2D).
		# link_entry_position: If owner is available and the owner is a NavigationLink2D, 
		# 		it will contain the global position of the link's point the agent is entering.
		# link_exit_position: If owner is available and the owner is a NavigationLink2D, 
		# 		it will contain the global position of the link's point which the agent is exiting.
	# / *******************************

	# the link that was reached
	var link:NavigationLink2D = details["owner"]
	
	# path that is the parent of the link
	var path:Path2D = link.get_parent()
	var path_through_building:Curve2D = path.curve
	var curve_points = path_through_building.get_baked_points()
	
	# we use a custom path to hold the navigation path and insert the curve points
	custom_path = agent.get_current_navigation_path() #this is a reference to the path
	#print("original current path: ", custom_path) # TESTING
	
	# we will get the current index of the path, and add that to the index of the different
	# curve' points. then store the current index
	var current_index = agent.get_current_navigation_path_index()
	for index in curve_points.size():
		custom_path.insert(index + current_index + 1, curve_points[index] + link.global_position)
	custom_path_index = current_index
	
	#when we enter a path we are inside a building
	inside_building = true
	steps_for_custom_path = curve_points.size()-4 # not sure why its 4. through testing it seems to work fairly well
	current_target_position = custom_path[custom_path.size()-1]
	print("new custom_path: ", custom_path) # TESTING
	#print("ping: ", agent.get_current_navigation_path()) #TESTING

I don’t think ClassDB.class_exists tracks class_name declarations, like get_class and is_class do not either.

The child script will only be of type GDScript, not your declared class, the node can be of type declared by it’s script, you need to use

if child is DenizenNAVIGATIONandMOVEMENT

Yes you are correct.

Thank you for your feedback. I was trying a lot of different things to understand where the problem could be, and I ended up using classDB.class_exists just hoping to where the problem was.

I don’t know how I was able to run similar scripts without this being a problem, and the lack of a previous issue is what probably sent my search down the wrong path.

I changed that and was able to access the functions I needed to. Thank you for taking the time to assist me.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.