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

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

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

In many games, you’ll want certain objects to be accessible from anywhere in your code. Think about the current level, the player character, or a UI manager. These are effectively global objects.

There are multiple ways to handle this in Godot:

  • Add scripts to autoloads so they’re always available.
  • Store references in some central manager.
  • Do get_tree().root.get_node("...") lookups every time you need them.

All of these work, but they can add complexity. There’s a simpler approach: use a static var instance inside the class itself.

The Static Instance Pattern

extends Node
class_name MyGlobalClass

static var instance

var instance_var: String

func _ready() -> void:
    instance = self
    instance_var = "hello world from instance"

Now, anywhere in your project, you can access it with MyGlobalClass.instance for example like this:

print(MyGlobalClass.instance.instance_var)

This gives you a global reference to the active instance of MyGlobalClass.

Why This Works Well for Global Objects

  • Automatic reference — You don’t need to pass references around manually.
  • No lookups — No more get_node() path strings scattered in your code.
  • Scoped global — Only exists when the class is active in the scene.
  • Readable — GameLevel.instance clearly communicates intent.

What Can You Do With It?

This pattern shines in a variety of everyday situations:

  • Resetting the level – If the player presses a retry button or you need to reload the stage, you can call the reset function of the current level directly, without routing signals or passing references around.
  • Accessing level properties – Things like difficulty, environment type, or custom rules can be read from anywhere in your code, since the active level is always available as a single reference.
  • Spawning objects into the level – When enemies, projectiles, or effects need to appear, they can be placed into the current level immediately, without worrying about where in the scene tree the level is located.
  • Coordinating with other managers – The current level can easily talk to other global-like managers (such as the player or UI), making it simple to trigger events like showing messages, playing sounds, or advancing the game state.