Game Development Community

Random Bullet Ricochet Noises

by Bryce · in Technical Issues · 04/10/2007 (7:56 pm) · 7 replies

Hello Everyone,

Here's the situation: I have an AK47 assault rifle in the hands of Kork. When he fires at me, bullet impacts will be everywhere around me. I have a bunch of bullet impact sounds (eight of them), and I am trying to get it to randomly choose a different sound out of these eight for each impact.

So, to clarify:

Instead of ping-ping-ping-ping when i hear the bullets smack my cover, i want a different ping to play each time. Anyone understand what I'm getting at?

#1
04/10/2007 (9:55 pm)
Just winging it here, but how about this...?

datablock ProjectileData(RicochetBullet)
{
   projectileShapeName = "~/data/shapes/weapons/bullet.dts";
   directDamage        = 20;
   radiusDamage        = 0;
   damageRadius        = 0;
   areaImpulse         = 0;
 (blah blah blah, it's a projectile)
};

// assuming you have RicochetSound1 through RicochetSound8 defined...

function RicochetBullet::onCollision(%this,%obj,%col,%fade,%pos,%normal)
{
   // Apply damage to all shape base objects
   if (%col.getType() & $TypeMasks::ShapeBaseObjectType) {
      %col.damage(%obj,%pos,%this.directDamage,"RicochetBullet");
  }

   ricochetSound = "RicochetSound" @ getRandom(1,8);
   playAudio(0,ricochetSound);

}

Dunno if that'll work (I haven't tested it), but it should give you a good starting point.

Have fun,

-- JohnDopp
#2
04/11/2007 (3:51 pm)
Hmm....I'm getting a parse error. Is playAudio() a default function, or will I have to define it?

The first thing I tried was to make a function called picRic(), which was close to what you had in mind. It would have "Ricochet", and then it would add the random number from 1-8. This would call the appropriate audioProfile. In the explosion datablock, instead of listing an audioProfile, I would enter in picRic() and I thought that every time there was an explosion, it would look at that datablock and call picRic each time there is an explosion. I thought that was pretty straightforward.

I think I've seen this done in Dark Horizons Lore, I may be mistaken.
#3
04/11/2007 (4:39 pm)
Let's start with playAudio().
That's a server-side function, so you should be calling it in a server script. It should be called on the object you want the sound to emanate from.

The parse error is because I didn't put a variable identifier in front of ricochetSound... it should be %ricochetSound.

So...

function RicochetBullet::onCollision(%this,%obj,%col,%fade,%pos,%normal){   
   // Apply damage to all shape base objects   
   if (%col.getType() & $TypeMasks::ShapeBaseObjectType) {      
         %col.damage(%obj,%pos,%this.directDamage,"RicochetBullet");  
   }   
  %ricochetSound = "RicochetSound" @ getRandom(1,8);   
  %obj.playAudio(0,%ricochetSound);

  // or, if you prefer...
  %obj.playAudio(0, picRic() ); 
  // where picRic() returns a random audioProfile name.
}

That should do the trick.

I don't believe you can set datablock values with a function like you're trying to do. Datablocks are defined on the server and are sent out to the clients to prevent the clients from tampering with the values. Because of this, you shouldn't change datablock values directly, or your server and clients will be seeing different things.

Because of this setup, your picRic() function would only be called when the datablock was initially created, not every time the projectile hits.

The ::onCollision method fires every time a bullet hits an object.
#4
04/12/2007 (4:03 pm)
This is strange. The console says it doesn't recognize PlayAudio(). I'll look up in my 3DGPAi1 book and see how it says to trigger the footstep sounds, maybe another function is in there. Thanks anyways for all the help.
#5
04/12/2007 (4:12 pm)
There's also alxPlay(), which is a client-side function I believe.

Are you calling playAudio() on an object, not by itself?
For example, %client.player.playAudio(0, someSoundDatablock);
#6
04/17/2007 (6:07 am)
I did %obj.playAudio(), %obj being the projectile.
#7
05/06/2007 (7:27 pm)
Okay....it works now.

%n = getRandom(7) + 1;
serverPlay3d("RicochetSound" @ %n, %obj.position);

i also commented out the lines that define the sounds in the explosion datablock so i don't hear two kapeews at the same time.