3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Pick a random sound to play out of a list  (Read 128 times)

« on: June 07, 2014, 12:05:31 PM »
For my zombie game I want multiple zombie sounds. The best way to do this would to make a script to pick a sound out of a list of sounds. But how can I do this?
« Reply #1 on: June 09, 2014, 03:00:13 AM »
int rand = iFloatRand(0,5);

if (rand == 0) iObjectStart(OBJ_0);
else if (rand == 1) iObjectStart(OBJ_22);
else if (rand == 2) iObjectStart(OBJ_44);
else if (rand == 3) iObjectStart(OBJ_66);
else if (rand == 4) iObjectStart(OBJ_88);


If the rest of your project is scripted, this is the way to go.


If you don't want to script, add an EventTimer with the timer set at zero, make this timer start all the sounds and check 'randomize target object'. Then just start this timer when you want a random sound.


Hope it helps.
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #2 on: June 09, 2014, 06:14:59 AM »
Would advice the same :D
If we don't give up we can achieve the unthinkable!
« Reply #3 on: June 09, 2014, 01:55:39 PM »
i see what you guys are intending here, and while the intended logic looks good, i think that there are gonna be a few problems...

first... the random generator part... isn't that going to spit back out a random float... 
the comparison tests below will then fail many times before getting a lucky match... just link a Value Print and you'll see what the actual results are...

second... the above code is going to load a bunch of sounds into a bunch of sound objects  but only one will be playing at one time...  it's be better to have only one sound loaded into memory at one time... wouldn't it...


--Mike

« Last Edit: June 09, 2014, 01:57:56 PM by Mike Hense »
« Reply #4 on: June 09, 2014, 02:20:28 PM »
first... the random generator part... isn't that going to spit back out a random float... 
the comparison tests below will then fail many times before getting a lucky match... just link a Value Print and you'll see what the actual results are...

Yep, but since the value is assigned to an integer, the decimals are cut off.

second... the above code is going to load a bunch of sounds into a bunch of sound objects  but only one will be playing at one time...  it's be better to have only one sound loaded into memory at one time... wouldn't it...

An alternative would be using iObjectRefresh(), which also works for sound effects these days. But refreshing an object every couple seconds can't be good for your framerate.
Rocket Rumble, a 3D Rad puzzle game:
http://www.3drad.com/forum/index.php?topic=9896.0
« Reply #5 on: June 09, 2014, 03:20:30 PM »
yeah, int variables seem to work with iFloatRand() and they appear to either cut off or round the fractional part...

iObject refresh using a single sound object seems to have little or no effect on the framerate...
seems better than loading up a bunch of unused resources... i can't confirm this though...

.oog files are quick and lightweight... seem to decompress quickly... and the change seems almost instantaneous...

i'm getting a constant 64 fps average on this old rig i have here...

anyway... try out the code below (substitute your own sound files) and tell me what you think...


Code: [Select]

int    choice=0, timer=0;

void Main()
{

   //                 get a new random number every 222 millisecs (change timer value as desired)
   if(timer>222){
     choice = iFloatRand(1,4);
     timer=0;
     OUT_22=choice;
   }

 
   
   //                 load new rand sound and play if timer is zero...
   //
   if(choice==1 && timer==0){
     iObjectRefresh(OBJ_0,".\\3DRAD_res\\objects\\soundsource\\data\\bluedanube1.ogg");
     OUT_0=44100;           // sets the frequency, they may all not be the same
     iObjectStart(OBJ_0);

   }

   if(choice==2 && timer==0){
     iObjectRefresh(OBJ_0,".\\3DRAD_res\\objects\\soundsource\\data\\h53helostartup.ogg");
     OUT_0=44100;           // sets the frequency, they may all not be the same
     iObjectStart(OBJ_0);

   }

   if(choice==3 && timer==0){
 //    iObjectStop(OBJ_0);
     iObjectRefresh(OBJ_0,".\\3DRAD_res\\objects\\soundsource\\data\\m60_gun_test_fire.wav");
     OUT_0=44100;           // sets the frequency, they may all not be the same
     iObjectStart(OBJ_0);

   }

   OUT_44=iFrameRate(false);
   timer++;
}


--Mike
« Last Edit: June 09, 2014, 03:43:24 PM by Mike Hense »
Pages: [1]