+5 votes

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!

in Engine by (87 points)

2 Answers

+5 votes
Best answer

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.

by (101 points)
selected by
I confirm that.
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!
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 ?
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 :)

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.

+4 votes

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.

by (861 points)
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.

Categories