MusicManager is an autoload singleton that extends AudioStreamPlayer. It centralizes all background music selection and playback state, so every part of the game changes or pauses music through a single, consistent API rather than manipulating audio nodes directly.
Because
MusicManager extends AudioStreamPlayer, it is the audio player — it is not a wrapper around one. The node itself plays the stream.Class overview
music_manager.gd
MusicManager loads a Playlist resource (playlist.tres) that maps music track names to AudioStream objects.
Initialization
_ready() validates that every entry in playlist.tres is a proper Object (a loaded AudioStream). Type mismatches are reported immediately at startup rather than at the moment a track is first requested.
API reference
play_music(music_name: String) -> void
Loads the named track from the playlist and starts playback immediately, replacing any currently playing stream.
The track name as registered in
playlist.tres. Case-sensitive. If the key is not found, _playlist.get_item() raises an error.switch_music_playing(on = null) -> void
Toggles, pauses, or resumes playback depending on the argument passed.
Controls the pause state:
null(default) — togglesstream_pausedtrue— pauses playbackfalse— resumes playback
push_error and returns without changing state.Toggle
Pause
Resume
Usage patterns
- Play music on scene load
- Switch tracks on scene change
The most common pattern: start the appropriate track in
_ready() of the scene’s root node.The Playlist resource
All track names are defined inplaylist.tres. Open this file in the Godot Inspector to view, add, or rename tracks.
Track names in
playlist.tres are the only source of truth for valid play_music arguments. Check the Inspector to see which names are registered before calling play_music.Adding a new track
Import your audio file
Add the audio file (
.ogg, .mp3, or .wav) to the project’s filesystem under res://assets/music/.Open playlist.tres in the Inspector
Locate
playlist.tres in the FileSystem dock and open it. You will see the existing name → AudioStream entries.Add a new entry
Add a new key-value pair: the key is your track name string (e.g.,
"credits_theme"), and the value is the imported audio file.Verify at startup
Run the project. If
check_item_types() logs no errors in _ready(), your track is correctly registered.Related pages
Autoloads
Overview of all autoload singletons and initialization order.
SceneManager
Transition between scenes — call
play_music after change_to_scene.FlagsManager
Use flags to determine which track to play (e.g., after tutorial completion).
Architecture overview
How autoloads fit into the overall project structure.