Page 1 of 1

Playing Sound Effects Randomly

PostPosted: Sun Jan 20, 2013 8:32 pm
by happyjustbecause
Hello everyone, I haven't been busy working with gameEditor for a while, so I feel like I should do something!

I've been wanting to add multiple "hurt" sound effects that way the same one isn't played over and over again. I now have multiple hurt sound effects, but I don't want to just have them play in an order like play sound effect 1 if health was 5, play sound effect 2 if health was 4, I want it to just play randomly from a list of sound effects. How can I do this? I know there is the rand function, but how can I do this with sound effects?

Re: Playing Sound Effects Randomly

PostPosted: Sun Jan 20, 2013 9:18 pm
by skydereign
Do you know how to use rand? At the base of every random action is a random number. Playing sounds is no different. Just thinking about it, how would you use a random number to choose which sound to play? If your sounds were ordered 0-4, you could use the random number to determine which one to play. That makes sense, so how would we set that up? Here is a way (not really the best, but it bypasses a problem gE has with sound files). Have a function PlayHurtSound or similar.
Code: Select all
void
play_hurt_sound ()
{
    int random = rand(5); // returns 0-4

    switch(random)
    {
        case 0:
        //PlaySound2 function call
        break;

         // and so on for cases 1-3
    }
}

Normally you would probably just put the names of the sounds into an array, and use the random number to index it, but gE requires the PlaySound2 function to have a literal string for a given sound file, or it assumes you removed it, and it won't be included in your game.

Re: Playing Sound Effects Randomly

PostPosted: Mon Jan 21, 2013 5:43 pm
by happyjustbecause
Thank you so much, I now have the sound effects all playing randomly. I want to do this with many things, animations and other sound effects that way you aren't expecting the same exact thing to happen.