How to play audio whilst changing scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By spicy
:warning: Old Version Published before Godot 3 was released.

Hi everyone,

Basically I have a main menu with two buttons (start game and exit game). I’ve created a sample player, a new sample library, imported/converted my wav files and then loaded the smp files into the sample library.

All I want to do is to be able to play the sound I’ve loaded in when I click my buttons. This does work (sort of) at the moment, except the sound isn’t played fully, it starts and then it is cut off. I believe this is because it quickly plays the audio and then proceeds to move on to the next line of code, effectively cutting it off during playback.

Here is my code for my start game button:

extends Node

func _ready():
set_process(true)

func _on_Start_Game_pressed():
get_node("../SamplePlayer").play("Sound1")
get_tree().change_scene("res://levels/level_one_game.scn")

And here is the code to my exit game button:

extends Node

func _on_Exit_Game_pressed():
get_node("../SamplePlayer").play("Sound1")
get_tree().quit()

Does anyone know a way around this? I asked when I was on the IRC and some body mentioned ‘ResourceInteractiveLoader’ and somebody else mentioned ‘threads’. I’ve never heard of either of these, and I’m not sure if they are the way to do it or not. Any help with this would be greatly appreciated, no doubt it’s something very simple.

Thanks!

:bust_in_silhouette: Reply From: thomas_laverne

I’m not 100% sure, but I think you need to play the music at a global singleton node (instead of a scene node). You can leanr the details of what is a global node here
http://docs.godotengine.org/en/latest/tutorials/step_by_step/singletons_autoload.html

Those nodes are always loaded, so they don’t care about scene nodes being loaded/unloaded.

I confirm that.

lukas | 2016-02-29 23:05

Hi thomas_laverne.

Thank you for pointing me in the right direction. I understand what you’re saying and how they can be autoloaded. The only problem is I’ve had no sucess in actually doing this. I believe this is the solution to my problem but I’m still not exactly sure how to do it.

I had a look at the link you provided, and I’ve created an autoload script which has a button on press function and plays the sound when pressed. I’ve stored that script in the autoload section in project settings, but after that I’m not exactly sure what to do. I’ve tried various other things but still no closer to getting it right, would you or someone be able to help me finish this last hurdle?

Also I’m not exactly sure by what you mean when you say ‘global singleton node’. Unless you just mean the script autoload name (which according to the link you provided me) is the name of the node also.

Thanks in advance!

spicy | 2016-03-02 14:15

Hi spicy,

Well I think you can put any ressource in Autolad and you can put a Node2D (and not only its script) in order to make sure it’s always loaded when your application start. Then you can design this node with fancy buttons and sample player for your needs. Note that global.gd extends from Node therefore you can edit whatever you want if you save it as a scene node.
Is it what you want ?

thomas_laverne | 2016-03-02 14:44

Doesn’t matter anymore, thanks to kubecz3k’s help on IRC I was able to figure it out and get it working in the end. You set me off in the right direction though, so thank you for taking the time to answer. Thanks :slight_smile:

spicy | 2016-03-02 16:31

Hi, sorry to revive a pretty old topic but can you share how you solved it?
I’ve been trying to do this myself.

Although, I’m using Godot 3.1.1 , i could still get hints to the right direction.

jagc | 2019-05-27 08:30

:bust_in_silhouette: Reply From: batmanasb

In my first Godot game I created a script that “extends StreamPlayer” and added it to AutoLoad. This way you can preload a song, “set_stream(song)”, and “play()”. This wouldn’t be part of a scene so it would keep playing between scene switches/loading.