The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

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?

in Engine by (3,938 points)
edited by

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

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.

1 Answer

+5 votes
Best answer

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").

by (29,360 points)
selected by

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


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.

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.

Hope I am not coming off as annoying.

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.

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:

    add_autoload('the node')

    remove_autoload('said node')

Maybe that's what I should have put in my question first :/

Here it is. Slice generator.tscn inside the plugin's folder is the Singleton.

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.

Ok sure. Thanks for the help.

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.

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 Frequently asked questions and 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 [email protected] with your username.