Adding a root node from a script

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

I’m writing an editor plugin, and I’d like to be able to add a root node (or change the root node) for the currently edited scene.
Is this even possible from GDScript? Adding child nodes is working just fine, but how do I add a new root?

How are you doing it? I’m actually trying the same thing, but I’m kinda lost even looking at the current addon examples.

henriquelalves | 2016-05-20 08:59

Hm, can you be a little more specific? What exactly are you trying to do?

DenisComtesse | 2016-05-20 19:07

Well, so far I’m trying to implement a side-dock addon that can manipulate the main scene nodes; but for some reason, the side-dock node is not loading its own scripts. When I connect a “pressed” action of one of the dock buttons to the main control node script of the dock, the terminal gives me an error every time I click the button “The method called by the signal doesn’t exist” when it clearly exists on the script. I don’t think this is directly related to your problem, but I’m trying to understand when and where the addons are actually loaded on the engine, because it looks like it’s in a completely different place than the current Tree node scene.
As a side note (and something that maybe you can use), I was able to find the current “2D world” scene by using a recursive search on the “/root” node (so I could draw stuff directly on the 2D interface): Recursive search for viewport on Godot · GitHub
I wasn’t the author (it was someone who answered one of my questions here on the Q&A), but maybe you can use it to find the Node Tree scene and hack yourself in adding a root node?

henriquelalves | 2016-05-20 20:24

Can’t really say anything about your problem without seeing what you do in these scripts. Here’s how I do it:
I don’t connect any buttons within the “dock” scene, instead I just use the actual EditorPlugin script to write the functions and connect the buttons. This way I can use the buttons to call EditorPlugin functions more easily.
However, what you’re doing should work too, so I don’t quite know what you’re doing wrong.

DenisComtesse | 2016-05-22 07:59

:bust_in_silhouette: Reply From: Gokudomatic2

Changing current scene
After a scene is loaded, it is often desired to change this scene for another one. The simple way to do this to use the SceneTree.change_scene() function:

func _my_level_was_completed():
    get_tree().change_scene("res://levels/level2.scn")

SceneTree Documentation

Thank you, but this is not quite what I was asking. As I said, I’m writing an editor plugin, so I want to add a root node to the currently edited scene. Not switching scenes inside the running game, but (for example) adding a new root node to an empty scene in the editor.

DenisComtesse | 2016-05-20 19:04

So far I know, there is only the root node and it’s created once. Scenes are kind of the root node of a scene. When you change scene, everything in the old scene is removed. But technically, the very first node is something you cannot manipulate like that from a script.

Gokudomatic2 | 2016-05-20 19:06

:bust_in_silhouette: Reply From: DenisComtesse

Someone posted the answer on github. For those interested, this code works:

var editor = get_node("/root/EditorNode")
editor.set_edited_scene(node)

Thanks jjay!