Make a node "root of tree" in the godot editor from GDscript

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

In the godot editor when you right click on a node in the tree you can select an option that says “Make this node the root node”.

How do I do the same from gdscript?

:bust_in_silhouette: Reply From: rossunger

You need to:
var NodeToRemove = get_tree().get_root().get_child(0) get_tree().get_root().remove_child(NodeToRemove) to remove the original root then get_tree().get_root().add_child(yourNewRoot)

If you want the removed child to be added back in as a child of the new root, then you need to store it in a variable when you remove it, and yourNewRoot.add_child(NodeToRemove) it back in after you’ve made a new root

If you’re trying to have this work in the editor as tool script or a plugin, then it’s a little more complicated… to do it the right way I think you need to make it a plugin, then you need to get_editor_interface().get_edited_scene_tree().remove_child etc…

There’s probably a hacky way to do it too…like owner.get_parent().remove_child() etc…

One thing to note, I think if you’re doing the editor thing, you need to set the owner of whatever node you’re adding to be get_editor_interface().get_edited_scene_tree(). otherwise it wont save with the scene (and I think it also just doesnt appear in the tree-view