Add_child not adding child

Godot Version

4.4.1

Question

Hi, I’m new to Godot and relatively new to coding in general, currently working on a drag and drop game.
In my _drop_data function I am trying to do two things:

  1. Check that the data being dropped is the appropriate item type
  2. Prevent the data being dropped if an item has already been dropped there

I’m running into an issue with #2. The rest of the code seems to be working as intended, but I can’t work out how to get the first elif statement working, aka get the console to print feedback when the droppable area is not empty. There are no debugger errors and It doesn’t appear to be dropping any data, but the console isn’t printing the relevant text either. I’ve hit a bit of a wall so any advice is appreciated. Apologies if I haven’t included enough info in the post.

@export_enum("HOTDOG:0", 
			"BURGER:1", 
			"TOMATOSAUCE:2", 
			"MUSTARD:3", 
			"LONGBUN:4", 
			"SHORTBUN:5") var slot_type : int

func _can_drop_data(_pos, data):
	return data is Control

func _drop_data(pos, data): #original parent is defined in case it needs to be removed
	var dragged_control = data
	var current_parent = dragged_control.get_parent()
	var current_child = get_child(0)
	
	if dragged_control.slot_type == 0 && current_child == null:
		add_child(dragged_control) 
		
	elif dragged_control.slot_type == 0 && current_child != null:
		print("There is already an ingredient cooking")
	
	elif dragged_control.slot_type != 0:
		print("This ingredient is not suitable for grilling")

(Google translator)
I think it’s a good idea to specify the object types in the methods. For example, so the compiler knows what type of object “data” is.
Put more “print” statements inside the first if statement to see if you’re looping through it and even outputting the current_child statement.
It seems like the first if statement is possibly executing, but the add_child statement isn’t working for you. If the dragged_control belongs to another scene, other than the add_child statement, I think you need to set the owner of the dragged_control to the new scene for dragged_control.owner = self to work.

Thanks, yeah it looks like add_child is not executing properly. Weirdly, I have pretty similar code on another node that is working as intended

func _drop_data(pos, data): #original parent is defined in case it needs to be removed
	var dragged_control = data
	var current_parent = dragged_control.get_parent()
	var current_child = get_child(0)
	
	
	if current_child == null && dragged_control.slot_type == 4:
		add_child(dragged_control)
		
	#prevent hotdog being dropped before bun
	elif current_child == null && dragged_control.slot_type == 0: 
		print("place a bun first")
	
	
	elif current_child.slot_type == 4 && dragged_control.slot_type == 0:
		current_child.add_child(dragged_control)
		print("hotdog added")
		print(current_child)

You may need to use reparent instead of add_child if the node already has a parent, which I figure it must for drag and drop to function.