play random audio onComplete

Here is some script I’m trying to use to get an audio.play to onComplete randomly to 1 of 3 sounds. For the purposes of testing the number is always 1.

act_bloop1 does not initiate…

I’ve used similar code to execute other functions successfully, but timerStash.timer_gl in this case is not even running act_bloop1 as you would expect (as GenLoop[1] does equal act_bloop1).

        local act_bloop1
        local act_bloop2
        local act_bloop3
        local genBloop
        
        local rn = 0
        local GenLoop = {}
        GenLoop[1] = act_bloop1; GenLoop[2] = act_bloop2; GenLoop[3] = act_bloop3
       
       act_bloop1 = function()
               audio.play(vars.bubloop1, { channel = 3, oncomplete = genBloop })
       end
       
       act_bloop2 = function ()
               audio.play(vars.bubloop2, { channel = 3, onComplete = genBloop })
       end
       
       act_bloop3 = function ()
               audio.play(vars.bubloop3, { channel = 3, onComplete = genBloop })
       end
       
       genBloop = function ()
               rn = 1 – just using number 1 for testing will use a random generator here
               timerStash.timer_gl = timer.performWithDelay( 0, GenLoop[rn], 1 ) – GenLoop will equal 1
       end

Of course - an easy solution, creating GenLoop[1] = act_bloop needs to happen after act_bloop is created.

Thanks.

How about an array of bloops:

bloop = {} bloop[1] = audio.loadSound("bloop1.wav") bloop[2] = audio.loadSound("bloop2.wav") bloop[3] = audio.loadSound("bloop3.wav")   local function playBloop()       local thisBloop = math.random(#bloop)         audio.play(bloop[thisBloop]) end   audio.play(someOtherSounds, {onComplete=playBloop})

Thanks belatedly Rob Miracle - loving your eloquent code, works a treat :slight_smile:

Of course - an easy solution, creating GenLoop[1] = act_bloop needs to happen after act_bloop is created.

Thanks.

How about an array of bloops:

bloop = {} bloop[1] = audio.loadSound("bloop1.wav") bloop[2] = audio.loadSound("bloop2.wav") bloop[3] = audio.loadSound("bloop3.wav")   local function playBloop()       local thisBloop = math.random(#bloop)         audio.play(bloop[thisBloop]) end   audio.play(someOtherSounds, {onComplete=playBloop})

Thanks belatedly Rob Miracle - loving your eloquent code, works a treat :slight_smile:

What is this line and what does it do?

audio.play(someOtherSounds, {onComplete=playBloop})

I know it plays the audio with audio.play - I would like to understand the rest of the line though. Please explain. Thanks.  :slight_smile:

Hi @Joe C.,

This just means that when “someOtherSounds” is finished playing (that being an actual sound file), then the function “playBloop()” is called. In the sample that Rob gives, this picks a random number from 1 to #bloop (the number of entries/indexes in the table “bloop”), then it plays the sound associated with that index number.

Brent

What is this line and what does it do?

audio.play(someOtherSounds, {onComplete=playBloop})

I know it plays the audio with audio.play - I would like to understand the rest of the line though. Please explain. Thanks.  :slight_smile:

Hi @Joe C.,

This just means that when “someOtherSounds” is finished playing (that being an actual sound file), then the function “playBloop()” is called. In the sample that Rob gives, this picks a random number from 1 to #bloop (the number of entries/indexes in the table “bloop”), then it plays the sound associated with that index number.

Brent