Topic was automatically imported from the old Question2Answer platform.
Asked By
Spafnar
Hello, previously I had asked a question about how to make my array of songs I had attached to an AudioStreamPlayer load and play a new song after a previous one had ended in game, because the game would otherwise just go silent. I eventually figured it out because I was unaware that there was a “finished” node I could connect. This is the original code:
var _music_list = ([“the songs i saved the paths for”])
var song = _music_list[randi() % _music_list.size()]
$AudioStreamPlayer.stream = load(song)
$AudioStreamPlayer.play()
The code I added which would allow it to keep going has since been deleted but it was pretty simply along the lines of
Now the issue with this is that it’s impossible to end the endless stream of music because when it stops it is told to play a new one. It worked well in game like I wanted it to, but because my gameover function tells the audiostreamer to stop, it starts playing anyways and overlaps with the main menu music. Is there a way to make it continually play songs in game, but definitely stop once it’s game over, and play when newgame?
You could have a variable that keeps track of whether you want the audiostreamer to not play music, like during game overs and stuff. It should be a boolean variable. Set the variable to false when the game is over and place the code to play a new song inside an if statement that checks if the variable is true in order to play a new song. Once you start a new game, just set the variable to true and call the function that plays a new song.
I’m not very good at creating booleans and I don’t do it very often. I tried make an onready var:
onready var musicplay = $AudioStreamPlayer.play()
and then put this if statement in the audiostreamfinished function:
if game_over():
musicplay = false
But it hasn’t changed anything. I like your idea and I think it would work but how should I make the variable you’re describing.
Spafnar | 2021-01-06 22:59
What does the game_over() function do? Does it end the game or does it check if the game is over? Instead of calling the function every time a song ends, you should just set musicplay to false when the game ends. Initialize musicplay to true instead of the return value of the play function, also.
exuin | 2021-01-07 01:23
I did it, but now i’m just shocked why it’s not working. I defined the onready var as
onready var musicplay = $AudioStreamPlayer.play()
and then i replaced both the “$AudioStreamPlayer.play()” lines with “musicplay = true” in both the audiostreamplayertimeout and the newgame function, and put the “musicplay = false” in the game over function. But now there is no music at all, even when removing the false script from game over.
Spafnar | 2021-01-07 06:55
You can’t set a variable to a function call like that. You should use a Boolean variable, which is literally either the word trueor false. You must set the value of the variable and call the play function separately.