+1 vote

I have a AudioStreamPlayer playing music, how would I make the music persistent between scene changes?

in Engine by (124 points)

2 Answers

+2 votes

Make a scene/script and add make it autoload, that way it will be added to the root, independent of the current_scene.

by (7,838 points)
+4 votes

I think singletons are what you're looking for. Singleton scenes and scripts will persist throughout the lifetime of your game, even when using change_scene(). You'll also be able to access them globally from all of your other scripts. So in your case, you'll want to have a music player scene set up as a singleton.

If you want to use your existing AudioStreamPlayer node, you can save it to a scene by right-clicking it and selecting "Save Branch as Scene" (you might want to give both the node and the scene a more identifiable name like "MusicPlayer", just so you can keep track of it better.) Alternatively, you can just make a new scene with a single AudioStreamPlayer node and save it the usual way. Either way, be sure to delete the node from its original scene, so that there won't be two music streams playing at the same time.

Now, to add the scene to your AutoLoad list. Navigate to Project > Project Settings > AutoLoad tab, then click the dotted button near the top-middle to open a file explorer. Double-click on your "MusicPlayer" scene, then click "Add" on the right-hand side. You should see your MusicPlayer scene added to the AutoLoad list, which means you should be good to go.

Don't forget that you can access your singletons from any script, with just the name of the AutoLoad (for example, MusicPlayer.stop()).

by

Incredible explanation. Thank you very much!

This was very helpful! Thank you

You are a hero to me for this knowledge. Good person!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.