Get_parent() or get_tree()

Question

Just a general question, is it better to use “get_parent()” or “get_tree()” when programming things like the main menu, who will instantiate another scene and add it to the tree before deleting itself.

I usually use “get_parent()” because those things are allways directly connected to the tree themselves, but it just ocurred to me, should I be using “get_tree()” instead? Does it even make a difference most of the time?

Thank you

When you get the parent, you discard anything below the parent. That’s not wrong as long as you’re not looking for a child that isn’t a child of the parent. Because in that case, the looking up will fail.

In this case i think use get_tree().change_scene_to_file() or get_tree().change_scene_to_packed() will be better, unless you want to create loading screens or manually control the scene change

You can look this to see what is better for you: Change scenes manually — Godot Engine (stable) documentation in English

That wouldn’t work, as the scene needs to recieve information from the one creating it before it becomes the new main scene

The current method I use (get_parent().add_child followed by queue_free()) works fine, I was just wondering if there is a standard way to do this I am not following

Oftentimes, if it works for you, it’s good enough. No need to dig deeper than you have to.

1 Like