changing continuous scene music via global?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ooopsies

Im trying to keep music playing through same-scene reloads, but change the music when going to the next scene. When the next scene starts i want the music to play continuously through the current scene reloads.

Right now the music restarts every time the player dies. I can make it work fine with autoloading ONE song, but i cant figure out how to apply it to multiple scenes. would love some pointers! thanks =)

:bust_in_silhouette: Reply From: njamster

From your description I’m not entirely sure what the problem is, but:

  1. Create a new scene with a node of type AudioStreamPlayer2D/3D
  2. Assign your music track as a stream and set autoplay to true
  3. Go to Project > Project Settings > AutoLoad and add the scene as a singleton

Now when you start the game, the music will play automatically. If you don’t want this, you could of course start it manually as well: just reset the autoplay-property to false. Because the music player is a singleton now, changing scenes will not restart the music anymore. If you change to a different scene, you can access the singleton by it’s name, stop the music, change the track and start playing again. So if you named your singleton “Music” it would look something like this:

Music.stop()
Music.source = load("<PathToMusic>")
Music.play()

thats what I’ve been doing, but i guess im confused on WHERE to call the singleton

Music.stop()
Music.source = load("<PathToMusic>")
Music.play()

where would this go? if its within a scene node the playback stops every time my character dies (and scene is restarted). i need the track to play continuously while in scene, but stop and change track for the next scene and again play continuously. repeat. sorry its hard to explain this haha. thanks for the much needed help.

Ooopsies | 2020-02-25 14:10

Attach the following script to your level:

var reload = false

func _ready():
	if not Music.playing:
		Music.source = load("<PathToMusic>")
		Music.play()

func _physics_process(delta):
	if Input.is_action_just_pressed("ui_cancel"):
		reload = true
		get_tree().reload_current_scene()
	if Input.is_action_just_pressed("ui_accept"):
		get_tree().change_scene("<PathToScene>")

func _exit_tree():
	if not reload: Music.stop()

So once the level enters the tree (and _ready is called) the music will start playing. Unless, there already is some music playing. When you press “ui_cancel” (defaulting to the escape key) you’ll reload the scene, pressing “ui_accept” (defaulting to the space key) you will instead to switch to a different scene. In the former case you’re setting a variable to true, that would otherwise be false. Once the level exits the tree (and _exit_tree is called), we check this variable and if it’s still false, we know that we’re changing and not just reloading the scene, so we stop the music. If the scene, we’re loading shares a similar script, it will start playing it’s own track instead.

njamster | 2020-02-25 16:02

So i just figured out my problem -
I fixed everything i needed to by changing my player position on death instead of reloading the current scene. Even though this convo wasnt the direct fix- it DID make me think differently and eventually fix the problem. Thanks a ton! loving these forums

Ooopsies | 2020-02-26 00:38