Singletons in Godot aren’t really ones, they are just nodes placed under the root of the tree. For example, it looks like this in a game:
- root (Viewport)
You can see this when running a game and toggling
Remote on the scene tree editor.
The Godot editor is built like a game. Which means it also has a SceneTree with a root, so it can also have autoload nodes that you can place at the same location simply using
get_tree().root.add_child(singleton_node) from within your EditorPlugin (you also have to take care of removing it if your plugin gets destroyed).
Here is a simplified view how the Godot Editor scene tree looks like:
- root (Viewport)
- Lots of editor UI nodes
- EditedScene (Viewport)
In Godot 3.0.x you won’t be able to access singleton nodes by name (which is a special GDScript syntax sugar that is being added in 3.1), but as long as you have access to the tree, I believe you will be able to get your node from anywhere using
For some very odd reason, I the node doesn’t run when I try running the scene. I then noticed in the remote scene tree view that it wasn’t there at all! And it runs perfectly fine in the editor. Meaning that the node is there in the editor, but gets removed when running the scene. And I checked. It is a direct child of the root node.
I’ll upload the current work so that you see what’s happening in the plugin.gd. which is inside
SIsilicon | 2018-06-05 17:07
Autoload nodes basically do automatically what I described manually in my answer. If you do that, of course they aren’t going to run in the game, because Godot is not told to instance them when you run the game. They are not getting removed, they are just not created at all (because remember, the game runs a second Godot instance without the editor tree to launch your game).
Now, if you want an autoload node to be present only in editor, in game ran from editor, but NOT on export… then I don’t know how you can do that. That would be either some code to do it manually a bit like I explained, or a feature request.
Zylann | 2018-06-05 20:02
I guess I’d have to think of something else. Hmm…
Wait export? Yes I’d want to run on export or else my addon would be useless.
My code needed is a slice generator that creates slices of the volume in the scene to be rendered. So I need to run pretty much anywhere. editor, runtime, and export.
SIsilicon | 2018-06-05 20:11
Hope I am not coming off as annoying.
SIsilicon | 2018-06-05 20:24
So… all you want is to run it everywhere?
Then just do what I said (so it will run in editor), AND add your node in project autoload (so it will run ingame and export). Maybe that should work fine for versions before 3.1.
Once 3.1 comes out, you should be able to use your singleton normally in the editor.
Zylann | 2018-06-05 20:55
Well yeah I could do that. But as a plugin I’d want to be able to add the node to project autoload via script. You know something like:
SIsilicon | 2018-06-05 21:06
Maybe that’s what I should have put in my question first
SIsilicon | 2018-06-05 21:32
Here it is. Slice generator.tscn inside the plugin’s folder is the Singleton.
SIsilicon | 2018-06-06 17:59
I told do what I know, you need to test the 3.1 master branch to see if it does what you want. I’m not sure about setupping the autoload automatically for the user, you could search for a feature request on this.
Zylann | 2018-06-06 18:53
Ok sure. Thanks for the help.
SIsilicon | 2018-06-06 21:15