Random beginner-question: does an instanced scene need to be removed again?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

Instancing Scenes in the docs (Nodes and scene instances — Godot Engine (latest) documentation in English) leave me a bit paranoid with one open question, because I read a lot about Node.queue_free and remove_child(node) and alike. I believe I can relaxe on this one but I’d like confirmation from the experts once and for all : )

My case: I have prepared a simple animation-scene (a circle expanding and fading out) that is put on top of any button withing my project when pressed.

func _on_Button_gui_input(event):
	if event.is_action_pressed("mousebuttonclick"):
		var buttonpresscircle = preload("res://button-press-circle-scenes/ButtonPressCircle.tscn").instance()
		add_child(buttonpresscircle) 

This works great. Every button features that code.
I don’t have to „remove“ anything after that little animation is being „called“, do I? It’s not „adding up garbage“ anyhow when pressing more and more buttons over time, right? Or should I somehow „delete“ the scene again right after it’s finished the animation?

:bust_in_silhouette: Reply From: Help me please

Each time you will press the button will add that animated scene. And soon you will discover that these are actually accumulating. To see it run your project press that button a few times. Then minimise that game window. Now in the inspector click on the remote button. Now just see there are many more nodes of the animation scene. Better if you delete all the animation node after the animation is finished either by queue_free()or by remove_child(node)

Yes, I can indded see them piling up in the remote scene tree.
Originally I didn’t even have a script attached to the instanced scene as the animation runs on startup one shot. Now I added a

func _ready():
    yield (get_node("ButtonPressCircleAnimPlayer"), "animation_finished")
    queue_free()

and the Node is being deleted.

Thanks for your help!

pferft | 2021-07-08 13:35