How to shuffle soundtracks?

__Aeon____Aeon__ Posts: 37Member
edited March 25 in Programming

I wish to loop between music tracks in an audio library, whenever a track finishes, another should start afterwards.

Each time music is required to be played this is called:

Node starts with:

var g_playing_music = false
var g_track_index = 0
var g_track_indexes_randomzied = []
var track_managing_signal


func _ready():
    track_managing_signal = _music_track_finished()

Then:

func _randomize_music_tracks():
    g_track_indexes_randomzied = []
    var l_index

    for i in range(5):
        l_index = randi() % 5
        while(g_track_indexes_randomzied.has(l_index)):
            l_index = randi() % 5
        g_track_indexes_randomzied.append(l_index)

Which should randomize the soundtrack indexes, followed by:

func _music_track_finished():
    g_track_index += 1
        if g_track_index == 5:
            g_track_index = 0
        track_managing_signal.resume()

func enable_game_music():
    g_playing_music = true
    _randomize_music_tracks()

    while(g_playing_music):
    _play_music_track(g_track_indexes_randomzied[g_track_index])
    yield(get_node("SamplePlayer2D"), "_music_track_finished")

func disable_game_music():
    g_playing_music = false
    get_node("Game Music").stop_all()

func _play_music_track(l_music_index):
    if l_music_index == 0:
        get_node("Game Music").play("Track1")
    elif l_music_index == 1:
        get_node("Game Music").play("Track2")
    elif l_music_index == 2:
        get_node("Game Music").play("Track3")
    elif l_music_index == 3:
        get_node("Game Music").play("Track4")
    elif l_music_index == 4:
        get_node("Game Music").play("Track5")

When music is to be toggled one the function enable_game_music() is called, what i don't know is how to use the yield signal to check when a track finished playing, since there aren't "special" signals for the SamplePlayer2D node.

Note: There's no signal called yield(get_node("SamplePlayer2D"), "_music_track_finished"). This is wrong.

Answers

  • __Aeon____Aeon__ Posts: 37Member

    Anyhow, i think i've got it. I have to use Ogg Audio Streams with the StreamPlayer which does have a finished signal.

Sign In or Register to comment.