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.

#GodotEngine #Godot4 #GDScript #GameDev

You Don’t Need Globals in Godot: Use static var

What's one way to handle Global Objects in Godot?

Alex Hortopan's blog

@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.

@bryce Are you able to point me to articles detailing the downsides? I'm interested to learn both sides so I can make the best decision for my game...
@ConditionalCoder It's mostly an organizational issue. There's nothing to enforce what script stores what data statically and where mutations can come from (any code can modify any static variable at any time). For simple things like "give me the active player character" where the gameplay design reinforces the program state (a game which promises to never have more than one player-controlled character simultaneously), it's fine. But it can quickly fall apart when you then have, say, character stats, positions (such as across scenes), inventory, etc. strictly limiting your program's growth and modularity.
@ConditionalCoder Seems like it boils down to if you want an instance to exist in one scene as a global, rather than a global defined in the project settings which exists in all scenes.
You can use this static instance if you want the 'global' to reset on get_tree().reload_current_scene() without any extra effort

@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.