The "Static Instance Pattern" seems like a bit of a hack... But is it actually a legitimate and useful way to use #Godot?
https://blog.hortopan.com/you-dont-need-globals-in-godot-use-static-var
Interested to hear thoughts.
The "Static Instance Pattern" seems like a bit of a hack... But is it actually a legitimate and useful way to use #Godot?
https://blog.hortopan.com/you-dont-need-globals-in-godot-use-static-var
Interested to hear thoughts.
@ConditionalCoder I feel the title is misleading. `static` as a keyword in this context *defines* a variable as global.
It can also lead to very messy code where you have to add more static variables to manage larger and larger global contexts. `static` is a great feature, but should be used sparingly IMO.
@ConditionalCoder To be clear: the static "singleton" instance pattern is one I've used plenty of times and I definitely think it can be useful (especially for things like grabbing the active player character Node or Camera), but it's upsetting whenever I see articles or videos proclaiming "this is the best option in all cases and you should always do this."
There are always downsides; there is no free lunch.
@ConditionalCoder in your specific example, if you ever add a second node of this type, it will override the static instance. You probably want a null check to ensure an actual singleton.
But in any case, Godot’s autoloads seem to solve the same problem in a better way.