When to use an autoload

Godot Version

4.2

Question

I’m trying to learn more about when i should and should not use an autoload. Right now, i’m using an autoload for managing the sound in my game. Basically i have an AudioManager autoload, and a “play_sound/play_music” functions, as well as a “stop_all_audio” function to stop all audio in situations where i want to do things like transition a scene, or play a new music track.

The documentation though states this is bad: Autoloads versus regular nodes — Godot Engine (stable) documentation in English

Pretty clearly it calls out exactly this thing:

Other engines can encourage the use of creating manager classes, singletons that organize a lot of functionality into a globally accessible object. Godot offers many ways to avoid global state thanks to the node tree and signals.

For example, let’s say we are building a platformer and want to collect coins that play a sound effect. There’s a node for that: the AudioStreamPlayer. But if we call the AudioStreamPlayer while it is already playing a sound, the new sound interrupts the first.

A solution is to code a global, autoloaded sound manager class. It generates a pool of AudioStreamPlayer nodes that cycle through as each new request for sound effects comes in. Say we call that class Sound, you can use it from anywhere in your project by calling Sound.play(“coin_pickup.ogg”). This solves the problem in the short term but causes more problems

This makes a lot of sense to me for many reasons. But it got me thinking, how would i manage making audio the responsibility of the nodes who want to play audio, and still have a way to say “stop all audio” from playing?

This makes me wonder when the autoload makes sense and when it doesn’t and how exactly this whole thing would work?

For a “stop all audio” you may want to create a new audio bus for transition scenes and fade out volume through the audio bus instead. You could group audio nodes based on if you want them to stop during a transition.