+2 votes

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?

in Engine by (18 points)

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

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

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): https://gist.github.com/henriquelalves/d1f687d7e73527762098921c1f116403
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?

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.

2 Answers

0 votes

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

by (698 points)

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.

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.

0 votes

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!

by (18 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.