Godot: I can create 2 singletons of same type.

Created on 23 Sep 2018  Â·  11Comments  Â·  Source: godotengine/godot

Godot version:

3.0.6
3.1-alpha1

OS/device including version:

Windows 7 SP1

Issue description:

2 'singletons' of same type can exist at the same time. That should not be possible with singletons and this is how documentation calls them. Maybe this behaviour is fine and docs need to stop call autoload 'singletons' to not confuse people familiar with OO design patterns.

Steps to reproduce:
Run repro project, check Scene and Output.

Minimal reproduction project:

https://github.com/hilfazer/Projects/tree/EngineIssues/Godot/SingletonNotSoSingle

documentation gdscript

Most helpful comment

so maybe just call the column "global var"

All 11 comments

Behavior is fine since autoloaded nodes (scripts or scenes) are just node instances added to the root viewport.

I agree that documentation should avoid calling these "singletons", maybe only mention that can be used as such.

This is not meant to make the script _class_ a singleton, just to create an instance that is globally accessible and always the same. It's an engine feature, not a GDScript feature.

I can see why people can conflate the meaning of the word "singleton" and think there can only be one of that type ever, but that's not really the "spirit" of the pattern. The idea of the pattern is simply to have a globally accessible instance of the type, it doesn't really matter if you have more instances or not.

I agree we should move away from the term "singleton" when referring to AutoLoads.

image

We're talking about the Singleton column title yes? Why not just change it to "active" or "enabled". Otherwise everything else seems fine.

We're talking about the Singleton column title yes? Why not just change it to "active" or "enabled".

The Singleton column just controls whether a global variable referring to the autoload is automatically created. If it's disabled, the autoload is still instantiated — in this case, it must be referenced using get_node() or its $ shorthand.

so maybe just call the column "global var"

oh err.... i was going to make a pull request but then realized that "Singleton" is translated to all the languages.... so now I'm hesitant to. how about we just explain more in the docs. http://docs.godotengine.org/en/3.0/getting_started/step_by_step/singletons_autoload.html

Maybe something like "Alias"? Well, I'm not really sure how this works. Is this a global variable referencing the autoload or is it an alias for get_node("<autoload_path>")?

It's not an alias to get_node() because get_node() does a path lookup, while the "singletons" are a direct reference to the node.

I can see why people can conflate the meaning of the word "singleton" and think there can only be one of that type ever

Yeah, they've probably read Gang of Four's or Jon Skeet's book. Or both.
http://csharpindepth.com/Articles/General/Singleton.aspx

This is now clarified in the documentation, closing.

Was this page helpful?
0 / 5 - 0 ratings