Playing Sound Effects Randomly

Non-platform specific questions.

Playing Sound Effects Randomly

Postby happyjustbecause » Sun Jan 20, 2013 8:32 pm

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?
For small creatures such as we the vastness is bearable only through love.
-Carl Sagan

Night Knight Development Thread
User avatar
happyjustbecause
 
Posts: 267
Joined: Tue Jul 26, 2011 3:10 pm
Location: Frazier Park, Ca
Score: 15 Give a positive score

Re: Playing Sound Effects Randomly

Postby skydereign » Sun Jan 20, 2013 9:18 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Playing Sound Effects Randomly

Postby happyjustbecause » Mon Jan 21, 2013 5:43 pm

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.
For small creatures such as we the vastness is bearable only through love.
-Carl Sagan

Night Knight Development Thread
User avatar
happyjustbecause
 
Posts: 267
Joined: Tue Jul 26, 2011 3:10 pm
Location: Frazier Park, Ca
Score: 15 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron