Godot: It's impossible to use singletons from tool script

Created on 5 Apr 2016  路  16Comments  路  Source: godotengine/godot

Operating system or device:
Ubuntu, Godot 2.01

Issue description (what happened, and what was expected):
When you define a script that have tool keyword on the top, and you use in this script any singleton it wont compile because of an error: (where "Global" is my singleton)
SCRIPT ERROR: GDScript::reload: Compile Error: Identifier not found: Global At: res://Gameplay/Levels/BaseLevel.gd:151. ERROR: reload: Method/Function Failed, returning: ERR_COMPILATION_FAILED At: modules/gdscript/gd_script.cpp:1894.
You won't be able to use singleton script with get_node("/root/Signleton") either.

Steps to reproduce:

  1. Define a "Global" singleton scene with a script
  2. Create another scene with a script in a tool mode, use "Global" singleton somewhere in the script.
  3. Restart Godot (needed for tool scripts to compile )
  4. Open scene with tool script
  5. Observe error in console, tool mode for your script is not working.

Link to minimal example project (optional but very welcome):

enhancement pr welcome editor plugin usability

Most helpful comment

Maybe we could add a flag "tool" to singletons (besides global) to get this to work (load them if the flag is present). If anyone wants to do this, please feel free to do it.

All 16 comments

@kubecz3k ~ is the singleton in tool mode too?

@bojidar-bg the error is the same regardless if the singleton is in tool mode or not (tried both).

i don't think singletons are loaded in editor mode, this is intended since singletons can also be scenes.
maybe you can do what you are intending to do in a different way?

Plugins would come in handy in this case...

@reduz Well in general I have a lot of 'helper' functions defined in singletons/globals. Those are the functions which I'm using in a daily basics and it's quite an issue for me... It would be ok for me if my singletons should be a tools as well... Maybe I should convert them to plugins as @bojidar-bg suggested but I'm not familiar with plugins api yet so I'm not sure if this is the way. In next week I will have time to explore all those fancy features that 2.1 will provide :)

I'm not sure if plugins can talk to each other. A way to make a set of plugins with common data would be an interesting feature.

I don't think this is a "bug" though, it simply wasn't designed this way.

Well the idea that difference tool keyword from plugins for me, is the possibility for every scene to have some small code that runs in editor, usually linked to getters/setters methods. Currently this is impossible because those scenes simply wont compile. While the plugins are more 'general use' or 'bigger' chunks of code.

Example project from #5763:

SingletonCompileError.zip
Open this test project, the error appears in the console.

Workaround: if your helper functions don't access the state of the singleton, you can make your functions static and do this in the script where the singleton is used:

# HOTFIX tool scripts cannot compile if they use singletons
const Groups = preload("groups.gd")

However if the script also uses another game-related singleton, this is becoming a problem, unless you can separate the tool-related part in another node.

Maybe we could add a flag "tool" to singletons (besides global) to get this to work (load them if the flag is present). If anyone wants to do this, please feel free to do it.

Not critical for the upcoming 2.1, so moving to the next milestone.

I'm having the same issue. Hope someone manages to make the change :)

I was trying to use a utility function in my singleton in tool mode and ran into this issue too.

I guess we should inject tool-mode singletons when starting editor in main.cpp :smiley:

It would definitely be useful to use singletons in tool mode to avoid cluttering a lot of scripts that are game objects as well as tools with preloads.

this doesn't look like it was added in 3.1

Was this page helpful?
0 / 5 - 0 ratings