How do I delete my node once I'm done with it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By funlamb
:warning: Old Version Published before Godot 3 was released.

I have flashing text that show the player which buttons they use for there paddles.
Once the flashing is done I want to delete the node altogether. When I run queue_free() or remove_child() I can still see the node in the inspector.

What am I missing? Here is the code. I know I make it into that block of code because in the inspector I can see that the hide() function has worked.

elif flash_opacity < 0:
	flash_times -= 1
	if flash_times == 0: # The number of times to flash has ended so kill the node
		flash_player_buttons = false
		hide()
		get_tree().queue_free()
		get_tree().remove_child(self)
	flash_up = true
:bust_in_silhouette: Reply From: funlamb

It’s not get_tree().remove_child(self). It’s get_parent().remove_child(self)

I found that queue_free() also works.

On a side note it also tried get_parent().queue_free() and that just deletes everything in your parent node. For me that was everything on the screen.

My new code now looks like this:

elif flash_opacity < 0:
    flash_times -= 1
	if flash_times == 0: # The number of times to flash has ended so kill the node
		flash_player_buttons = false
		hide()
		# get_parent().remove_child(self) # this also works
		queue_free()
	flash_up = true

Use just free() for nodes that are not on the tree (in case you have one).

eons | 2017-06-09 13:34

I was trying just calling queue_free() but it didn’t remove it instantly from the tree…
and would cause an error if I didn’t yield("idle..") for what I was doing

Yes, calling queue_free and get_parent().remove_child together worked well

rocket 007 | 2022-11-18 05:32

:bust_in_silhouette: Reply From: Zylann

queue_free is the safe standard way to destroy a node, and must be called on the node, not on the scene tree (because destroying the scene tree will destroy everything).