Game Development Community

Adding Sound to Weapons

by Dennis Pharr · in Torque Game Engine · 04/14/2002 (2:37 pm) · 3 replies

I have not seen this topic covered extensively in the forums yet, so I thought I would add this short tutorial for adding sound to weapons. It took me a while to figure this out and I only did so after reading through the forums for other posts that related to making the engine play sounds for certain events.

Certainly this method may not be the only way to add sounds to weapons, but it does seem to work. I want to say thanks to Joel Baxter, Jared Hoberock and Stefan Beffy Moises for their earlier posts on this subject.

Simple Sound Question

First, add the RifleFireSound AudioProfile Datablock to your /example/fps/server/script/rifle.cs script file:

//projectile fire sound
datablock AudioProfile(RifleFireSound)
{
filename = "~/data/sound/weapons/grenade.wav";
description = AudioDefault3d;
preload = true;
};


I used a grenade sound I had available that sounded good for the rifle sound. You can use any .wav file you wish. You may have to create the /weapon subdirectory or place the sound in another directory as you choose, just make sure the location of the .wav file agrees with your directory designation used in the datablock above.

Next, add the following line (in Bold) to the RifleImage onFire function which is near the bottom of the rifle.cs script file. I added the line following the projectile object creation function as shown below.

function RifleImage::onFire(%this, %obj, %slot)
{
.
.
// Create the projectile object
%p = new (%this.projectileType)() {
dataBlock = %projectile;
initialVelocity = %muzzleVelocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
ServerPlay2D("RifleFireSound",%obj.getTransform()); //play rifle fire sound
.
.
}

The ServerPlay2D function plays the sound and uses the AudioProfile "RifleFireSound" as the audio source. The %obj.getTransform() function returns the last known location of the soundsource the rifle in this case. The location of the sound source is necessary for the engine to know where in the world to play the sound.

#1
04/14/2002 (6:14 pm)
Ok.
this is not necessary.
ShapeBaseImageData class operates a state
the states are to be defined in the script
this is a Fire state defined for a weapon
take note of the variables used in this script
datablock ShapeBaseImageData(ColtPistolImage)
{
....
   stateName[4]			= "Fire";
   stateSequence[4]		= "Fire";
   stateSpinThread[4]		= FullSpeed;
   stateRecoil[4]		= LightRecoil;
   stateAllowImageChange[4]	= false;
   stateScript[4]		= "onFire";  
   stateEmitter[4]             	= ColtFireEmitter;
   stateFire[4]			= true;
   stateSound[4]		= ColtFireSound;
   stateTimeoutValue[4]		= 0.5;
   stateTransitionOnTimeout[4]	= "checkState";
   stateEmitterTime[4]		= 0.3;
....
}

notice the use of each variable.
the ColtFireSound variable leads to the playing of this sound by the state owner. your best bet is to study the StateData struct defined in the ShapeBase header file
#2
04/15/2002 (9:51 am)
Thanks for setting me straight on this Badguy.

I see now how the state machine works to play the sounds. In shapebase.cc, the playAudio function is called:

void ShapeBase::playAudio(U32 slot,AudioProfile* profile)
{
AssertFatal(slot < MaxSoundThreads,"ShapeBase::playSound: Incorrect argument");
Sound& st = mSoundThread[slot];
if (profile && (!st.play || st.profile != profile)) {
setMaskBits(SoundMaskN << slot);
st.play = true;
st.profile = profile;
updateAudioState(st);
}
}

which then calls updateAudioState(st)-

void ShapeBase::updateAudioState(Sound& st)
{
if (st.sound) {
alxStop(st.sound);
st.sound = 0;
}
if (st.play && st.profile) {
if (isGhost()) {
if (Sim::findObject(SimObjectId(st.profile), st.profile))
st.sound = alxPlay(st.profile, &getTransform());
else
st.play = false;
}
else {
// Non-looping sounds timeout on the server
st.timeout = st.profile->mDescriptionObject->mDescription.mIsLooping? 0:
Sim::getCurrentTime() + sAudioTimeout;
}
}
else
st.play = false;
}

updateAudioState(st)- actually plays the sound and also uses the getTransform() function to play the sound at the correct location in the world.

Thanks for the correction. I learned a great deal from this.
#3
04/15/2002 (5:42 pm)
good stuff :)
glad to help

I think the state methods in ShapeBaseImageData are great :)