Is there a way to add another singleton after the game was already loaded?

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

I want to add a new singleton, loaded from my server, after the game started.
I don’t want to just instance a node, because I want to take advantage of the short naming.

Is it possible?

Do you mean you don’t want to use AutoLoad?

gioele | 2021-06-09 19:01

:bust_in_silhouette: Reply From: DaddyMonster

No, it’s not possible. You have to set up singletons beforehand with autoload. I don’t see why anyone would ever want to create them on the fly. I certainly wouldn’t be in favour of Godot implementing this functionality. You have to be really careful with global variables, many coders shun them altogether and those that do use them with extreme care, as you can easily end up with spaghetti code. Making whole singletons on the fly sounds like a nightmare scenario.

“I want to take advantage of short naming” - what do you mean by this?

The motivation is, due to Play store’s extremely long review times, to be able to update the singleton code without releasing a new revision to the store.
So it’s not really creating it “on the fly”, but it’s creating it only once, after the framework was loaded and before the game itself starts.

Short naming - instead of using a singleton it’s possible to just instance a node once, and refer to it by its full path in the tree.

avnih | 2021-06-13 09:49