Switch background music after changing scene

Godot Version

v4.2.2.stable.mono.official [15073afe3]


Hi everyone, I’m new in Godot and this is my first post. I’m trying to change which music is playing in the background but can’t find a proper way to do it and searched a lot before coming here.
For exemple: I have the menu scenes musics and a main scene music. So I tried do get the current scene through the get.tree and using conditions, signal etc but didn’t find a proper way to update the info receive and change the music once the scene is changed. I’m only able to play one song in all project instead of only a fews. How can I keep monitoring the current scene so the correct music will be played? I wanna do this in a single scrip instead of putting one song at a time in all scene.
This is not my native language, so forgive me any erros! :slightly_smiling_face:

found someone with a similar issue who had been given a solution
How to get the value of a variable from another scene?
hope this helps

1 Like

Looking at it made me think more and helped me to get a conclusion. Here’s what I did:
I have two autoloads scripts, GameManager and MusicManager:

# On GameManager.gd
var current_scene = null

Set the function conditioning the current scene in MusicManager to play the specific music and then I use the game manager in the following scenes:

# Menu.gd
func _ready():
	GameManager.current_scene = "Menu"

And did the same for the others scenes and it worked. But surely theres a better way to do it, cuz what if I have several scenes? It will be a lot of work. :thinking: But for now this is working!

Thank you for the help!!