Game Development Community

Randomized Fire Sounds?

by Chris "C2" Byars · in Torque Game Engine · 10/10/2005 (4:24 pm) · 20 replies

Wondering if anyone implemented random firing sound effects for weapons.

For instance, the weapon script will pick randomly one of a choice few audio datablocks/files to play each time it fires.

I'd like to make it pick one of two, randomly.

Thanks in advance :)

#1
10/10/2005 (5:12 pm)
Done that a while ago, was basically a hack in the onFire function. Randomized a value between 1 and 5 and picked a sound datablock depending on the return. Should be easy.
#2
10/11/2005 (2:36 pm)
Well I put a getRandom in the onFire function of one of my weapons:

function XM8Image::onFire(%this, %obj, %slot)
{
   %fireSound = getRandom(1,2);

   echo("The value is " @ %fireSound);

That picks between 1 and 2. However if I stick a nice little if then statement in there, I get a syntax error:

function XM8Image::onFire(%this, %obj, %slot)
{
   %fireSound = getRandom(1,2);

   echo("The value is " @ %fireSound);

   if (%fireSound = 1)
   {
      stateSound[3] = XM8FireSound;
   }
    else
   {
      stateSound[3] = XM8FireSound2;
   }

So I'm not sure where to go after I get the random number between 1 and 2. :/
#3
10/11/2005 (2:52 pm)
I ain't no coder, but I don't see a closing brace used on the entire ::onFire function, the function seems open ended?? And would you need to do a return %fireSound; to get out of the routine? I'm having trouble getting a random number to trigger a playThread....my issue is that if the random number is the same twice, or more, the thread doesn't play until a new random[other than the last] is generated?? Good luck.
#4
10/11/2005 (2:54 pm)
Ah, I got it:

function XM8Image::onFire(%this, %obj, %slot)
{

   %fireSound = getRandom(1,2);

   if (%fireSound = 1)
   {
      %XM8SoundEffect = XM8FireSound;
   }
   else
   {
      %XM8SoundEffect = XM8FireSound2;
   }

Then I changed the stateSound[3] in the weapon's datablock to %XM8SoundEffect and it worked. :)
#5
10/11/2005 (5:13 pm)
Now for some odd reason it is not playing anymore. O_o

I tried just setting %XM8SoundEffect equal to XM8FireSound and setting stateSound[3] to %XM8SoundEffect, but that didn't work anymore either. Strange.
#6
10/12/2005 (1:51 pm)
As my bud Ari Julius puts it:

"You seem to suffer a series of extremely weird problems my friend"
#7
10/15/2005 (2:09 pm)
Anyone know of a working way to do what I need? :/
#8
10/15/2005 (4:41 pm)
function XM8Image::onFire(%this, %obj, %slot)
{

   %fireSound = getRandom(1,2);

   if (%fireSound == 1)   /// <- was missing an = sign
   {
      %XM8SoundEffect = XM8FireSound;
   }
   else
   {
      %XM8SoundEffect = XM8FireSound2;
   }

   ...

} /// <- needs closing brace if there isn't any more code in the function
#9
10/15/2005 (8:03 pm)
Meh. It still is silent when the weapon is fired.

No sound plays at all for the fire sound. No errors or relevant messages in the console.

The audio datablocks are valid, as they work if I directly state them in the stateSound[3] area.. but once I stick that percent symbol in there, it silences itself. No fire sound whatsoever. :/ Very strange.
#10
10/16/2005 (7:06 am)
Just tried it on a vanilla TGE, and there is no sound played for the fire state, as I thought.

I swore it worked for a while that one day I did it. I tested it. I heard the random sounds. Not sure what I did different though. :(
#11
10/17/2005 (1:43 pm)
Maybe instead of

%XM8SoundEffect = XM8FireSound;

something like this

%this.stateSound[3] = XM8FireSound;
#12
10/17/2005 (2:03 pm)
Negative on that one.
#13
10/17/2005 (2:08 pm)
I have a splitting headache since I broke my glasses the other day, but other than having a lapse I think


%this.stateSound[3] = XM8FireSound;


and


%this.stateSound[3] = XM8FireSound2;

should work... though it might change it for the next sound and assuming you kept state [3] as your fire one... though you might run into problems from server to client. You could do a serverPlay3D() for the sound possibly
#14
10/17/2005 (2:23 pm)
It'd be nice if Stefan Lundmark would share his solution, since he already got this working a while ago.. :P
#15
10/17/2005 (3:11 pm)
For mine I just took statesound out and called an %obj.playAudio in the onFire function. Amazing how hard you people are making this on yourselves. :P
#16
10/17/2005 (3:50 pm)
LOL it works perfectly.

Thanks a lot Jacob. :P

//in the ::onfire function

   %fireSound = getRandom(1,2);
   if (%fireSound == 1) 
  {
      %obj.playAudio(0,XM8FireSound);
   }
   else
   {
      %obj.playAudio(0,XM8FireSound2);
   }

Unfortunately, it isn't perfect and skips sound effects quite a lot, even if the sound file is shorter than the fire state's duration.

Time for me to look into weapon looping sound effects.
#17
10/24/2005 (3:35 pm)
What is strange about the above code, is that the sounds will skip/lag behind, sounding very stupid. However if there is just the one sound stated in stateSound[3], with no random code or anything, it won't lag behind at all and will play perfectly.
#18
10/25/2005 (3:17 pm)
Try making two fire states with different stateSound, like:

stateName[3] = "Fire1";
stateTransitionOnTimeout[3] = "Fire2";
stateScript[3] = "onFire";
...
stateName[4] = "Fire2";
stateTransitionOnTimeout[4] = "Reload";
stateScript[4] = "onFire";
...
#19
10/25/2005 (4:10 pm)
Yeah, that should fix the sound lag, may add fire lag though.
#20
10/25/2005 (4:11 pm)
That wouldn't make them random, though.