How to make an event play random sound clip?

Dell7730Dell7730 Member, PRO Posts: 388

I would like an event to play a different audio every time from a few selection

Best Answer

  • KevinCrossKevinCross London, UKPosts: 1,894
    edited June 2015 Accepted Answer

    I don't think you can play a random sound on a single line like you can show a random image so try it with a Change Attribute and a Rule i.e.

    Change Attribute: game.randomSoundNumber = random(1, 5)

    Rule: If game.randomSoundNumber = 1
    Play sound 1
    Rule: If game.randomSoundNumber = 2
    Play sound 2
    Rule: If game.randomSoundNumber = 3
    Play sound 3
    etc.

    Where I've got Play sound 1 and Play sound 2 is where you'd put the play sound behavour and then pick the sound file you want to play.

Answers

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2015

    Expression access to the play sound behaviour would really open up a world of possibilities. It would be great to name sound effects FX1, FX2, FX3 . . (and so on) and then being able to do things like play sound "FX"..(random (1,8))

  • Dell7730Dell7730 Member, PRO Posts: 388

    IT WORKS!!!!! > @KevinCross said:

    I don't think you can play a random sound on a single line like you can show a random image so try it with a Change Attribute and a Rule i.e.

    Change Attribute: game.randomSoundNumber = random(1, 5)

    Rule: If game.randomSoundNumber = 1
    Play sound 1
    Rule: If game.randomSoundNumber = 2
    Play sound 2
    Rule: If game.randomSoundNumber = 3
    Play sound 3
    etc.

    Where I've got Play sound 1 and Play sound 2 is where you'd put the play sound behavour and then pick the sound file you want to play.

    IT WORKS! THANKS

  • Dell7730Dell7730 Member, PRO Posts: 388

    Follow up, is there a way to make sure it won't repeat a clip until all the other choices have been used? The code works, just that sometimes it repeats some of the choices.

  • ThoPelThoPel GermanyMember, PRO Posts: 184

    @Dell Agarpo Why do you not use a variable, e.g. SoundCounter ("FX"..game.SoundCounter) and a count it up everytime. If SoundCounter reach Maximum just set it back to 1.

    Okay it is a list of sounds and not random, but it won't repeat the same sound multiple times. You could also random the first sound, e.g. you have 10 sounds, then don't set it back to 1 if it reach the maximum, just random(1,9). In the worst case you have multiple times sound9, sound10, sound9, sound10,...

  • SocksSocks London, UK.Member Posts: 12,822

    @Dell Agarpo said:
    Follow up, is there a way to make sure it won't repeat a clip until all the other choices have been used? The code works, just that sometimes it repeats some of the choices.

    http://forums.gamesalad.com/discussion/68841/random-numbers-no-repeating

Sign In or Register to comment.