Unexpected behavior: Nonexistent function 'begin_dragging' in base 'Nil'

Godot Version

4.2.2.stable

Question

I am making an inventory system, where UIInventory class is referencing UIDraggedItemRepresentation class. The following is the relevant portions of the UIInventory class:

extends Control
class_name UIInventory

@export_group("References")
# Other variables here
@export var dragged_item_representation: UIDraggedItemRepresentation

# Some other stuff

func _process(_delta):
	if inventory_open:
		if dragged_from_item == null:
			if Input.is_action_just_pressed("primary"):
				var container_over := get_container_over(get_global_mouse_position())
				if container_over:
					var item_over := container_over.get_item_representation_over(get_global_mouse_position())
					if item_over:
						dragged_from_item = item_over
						dragged_item_representation.begin_dragging(item_over.global_position, item_over.container, item_over.item_index)
		else:
			# the rest of the code

The begin_dragging() function is properly defined in the UIDraggedItemRepresentation, I am certain of that. I have set the dragged_item_representation from the inspector, but I keep getting the goddamn

Invalid call. Nonexistent function 'begin_dragging' in base 'Nil'.

error every single time for no reason. The dragged_item_representation resets to null just before calling the dragged_item_representation.begin_dragging() function for some godforsaken reason that is far beyond me to comprehend. The same issue happens when I get the node directly with get_node like so:

get_node("Item Representation").begin_dragging(item_over.global_position, item_over.container, item_over.item_index)

And yes, that node is the direct child of the UIInventory node.

I have encountered a similar problem recently in the same project, that time with an Array. I think that this might be an internal godot interpreter error. Both times happened after I did some code refactoring, specifically utilizing the “Replace in Files” feature. I would report this as a bug, but I want to make sure I am not missing anything obvious like I usually do.

Did you rename UIDraggedItemRepresentation class? I’ve had some weird issues when changing class_names. Check your global_script_class_cache (in .godot folder) and see if UIDraggedItemRepresentation is defined correctly. And if you moved files in the FileSystem dock while refactoring, you should check if there are any broken dependencies in UIDraggedItemRepresentation or related scenes.

1 Like

Hi Octopus, thanks for your help. I’ve figured it out, though. Just as I suspected, it was something really stupid that I’ve missed. For some reason the UIInventory script was attached to another node. I don’t know how or why, I think I saw a similar problem that someone else has had after refactoring their code- very strange. I suppose for those who have a similar problem, make sure your script is not duplicated somewhere else for some reason.