[SOLVED??]Creating a singleton from script.

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

I am creating a plugin that requires an autoloaded script. So that it can run from the editor and on runtime. What is the way?

do you want to run your autoloaded script in editor?
in that case, it’s upcoming feature with 3.1 I guess.

volzhs | 2018-06-04 23:00

Well more generally I want a script that runs in the editor, and in runtime. Which mean can’t be in editor plugin script itself since that only runs in the editor. I also don’t want the node to be in editable part of scene. You know like a Node that the user could just edit. They shouldn’t.
So I was thinking a singleton could do the trick since you don’t usually see a singleton in your scene until you run it. Unless of course there is a different solution.

SIsilicon | 2018-06-05 00:18

:bust_in_silhouette: Reply From: Zylann

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)
    - AnAutoloadNode
    - CurrentSceneRoot
        - SomeSpriteInGame
        - ...

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)
    - PlaceToAddYourSingleton
    - EditorNode
        - Lots of editor UI nodes
        - EditedScene (Viewport)
            - YourEditedScene

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 get_node("/AnAutoloadNode").

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 res://addons/volume/

#proudofgpuvoxelizer

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:

_enter_tree():
    add_autoload('the node')

_exit_tree():
    remove_autoload('said node')

SIsilicon | 2018-06-05 21:06

Maybe that’s what I should have put in my question first :confused:

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

Any update on this, especially for 4.0? Tried searching around and can’t find anything aside from this. Specifically adding an autoload to the project settings via script when setting up a plugin.

EDIT: Found this, which should solve this issue more entirely.
Godot : how can i add a script to autoload only in script? - Stack Overflow

navett52 | 2022-12-31 19:40