Tutorial: Singleton Music Node in Godot Engine 10/18/15
godot tutorial

In this tutorial we're going to use the Autoload functionality in Godot Engine to play music on the background without cuts between scene changes.

Autoloaded Scenes

Godot Engine has a functionality for creating Autoloaded Singleton scenes. That means you can tell Godot to load a scene automatically and only once, when the game is loading. That scene will remain on the SceneTree of the game even when switching between scenes.

We will use this to have a single stream of music playing on the background of every scene of our game.

You can check the official tutorial on Singleton Scenes on the Godot Wiki.

How to do it

  • Create a new scene on your project. This will be our autoloaded scene
  • Add a StreamPlayer as the root of the scene.
  • On the StreamPlayer properties, click on Stream > Load and load the music file.
    Don't forget to check the Loop and Autoplay checkboxes. This will autoplay the music on loop when the scene is loaded.
  • Now go to the Autoload panel. Click on the menu Scene > Project Settings and go to the Autoload tab
  • To create a new autoloaded scene enter the name the node and then click on the .. button to choose the scene you want to load.
  • Finally click on the Add button to add the autoload.

That's it! Now we have the music playing on the background even if we switch between scenes.


Let's take a look at the scene tree of the game while is running:


In this example we have first the "scene A" and then we switched to "scene B". Notice how the MusicPlayer scene remains loaded even when we switch scenes.
If you want to inspect the scene tree of you game go to the scrips view and then Debug > Show debugger. Then go to the Info tab and click on the refresh icon.


That's all for now. If you found this tutorial hard to follow or if you are just curious I will leave an example project in the download section below.

Bye!