How do I make my "music list" pick a new song after ending?

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

Hello, in my newgame function, I have the following script which allows me to utilize an array and have the engine randomly select a song and play it:

var _music_list = (["the songs i saved the paths for"])
var song = _music_list[randi() % _music_list.size()]
$ = load(song)

The issue is that after playing for long enough, after the song ends, it is just quiet. I orginally had added

if $AudioStreamPlayer.stop() 
              $ = load(song)

I know that the stream is never manually told to stop so I didn’t think this would work, nevertheless, it still remains quiet, but I dont get any warnings or errors. Does anyone know if there is a code that would allow the engine to randomly select another song after the current one ends?

:bust_in_silhouette: Reply From: Spafnar

Aha, nevermind, I just realized the AudioStreamPlayer has a “finished” signal I could connect. I leave this up if anyone else is curious.

Another Edit: Just kidding I still need help. It works like I want it to now, but because my game over function tells the streamplayer to stop, after it gets to main menu it plays a song and it overlaps with the main menu music. Is there a way to only have it load a new song to play once done only while the game is playing

If you’re using a variable for the game state such as “var playing = true” you can use that for playing/stopping the music. So if playing is true, the music keeps picking new songs after one is done. If playing is false (in the menu screen), the music list stops and you can just play the menu music.

Arakir | 2022-04-07 00:23

I made an addon that would be able solve both problems. Godot Playlists - Godot Asset Library

With this you could use an AudioStreamPlaylistPlayer and set continuous_play to false on finishing the game before stopping the player.