Engine.has_singleton() and Engine.get_singleton() Not Behaving as Expected

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

Hey All,

I have some Autoloaded Singletons added from an EditorPlugin node as well as one that I set up in the editor as a test.

In the ready function of some node, I have this:

if Engine.has_singleton("NewScript"):
		print('yup')
	else:
		print("nope")

NewScript is definitely there under Project Settings > Autoload. Am I confused as to how this function works? If so, how would I go about checking if a singleton exists?

Are the autoloaded singletons enabled?

Ertain | 2021-02-12 09:55

:bust_in_silhouette: Reply From: hilfazer

Those function operate on built-in engine singletons like Engine or Input, not on user’s autoloads.

print(Engine.has_singleton("Engine")) # prints 'True'

For accessing autoloads one can use the name from the first column of autoload settings, e.g.

NameFromFirstColumn.myFunction()

It only works if “Singleton” column is checked (it is by default).

Since autoloads are direct children of root their existence can be checked like this:

print( $"/root".has_node("NameFromFirstColumn") )

Naming of “Singleton” column in autoload settings is unfortunate. A better name would be “Global” as it adds a global reference. It wouldn’t confuse people as much.
I’ve opened an issue on github for this:

:bust_in_silhouette: Reply From: MintSoda

If anyone clicks in from search engines.
To access the scene tree from any Object, especially in plugins or EditorScript, use:

Engine.get_main_loop().root

So you can Engine.get_main_loop().root.get_node("AutoloadName")