Weapon Sound Tutorial
by Daniel Neilsen · 11/20/2001 (5:11 pm) · 6 comments
Download Code File
This is the first tutorial I have written but it is something that I believe a lot of
developers are having trouble with or would find useful so, I decided to get off my
slack ass and right a little tutorial for it. Hope it helps someone.
This tutorial does not require any engine modifications whatsoever and only
alters the game script files. In saying this, it should easily work with all versions
of the torque engine.
In this tutorial, any instructions that require editing will be prefixed with ****
WEAPON IMAGE SOUNDS
I will not go into sound effects in this tutorial but will rather concentrate on
the making of sounds in a weapon image. You can edit autoprofiles.cs and make
whatever distances you feel fit in there :)
Firstly, lets add a couple of sounds into the game
**** Open the file example/fps/server/scripts/rifle.cs
**** In or near the top of the file add the following lines
datablock AudioProfile(RifleMountSound)
{
filename = "~/data/sound/buttonOver.wav";
description = AudioClosest3d;
preload = true;
};
datablock AudioProfile(RifleFireSound)
{
filename = "~/data/sound/testing.wav";
description = AudioDefault3d;
preload = true;
};
datablock AudioProfile(RifleDryFireSound)
{
filename = "~/data/sound/footfall.wav";
description = AudioClose3d;
preload = true;
};
It is very important that these datablocks are defined BEFORE you define your weapon
image otherwise an error will be sent to the console and it simply wont work
**** Find the RifleImage datablock. The changes I have made are followed by // **** Added in sound tutorial
datablock ShapeBaseImageData(RifleImage)
{
// Basic Item properties
shapeFile = "~/data/shapes/rifle/weapon.dts";
emap = true;
// Specify mount point & offset for 3rd person, and eye offset
// for first person rendering.
mountPoint = 0;
offset = "0 0 0";
eyeOffset = "0.1 0.2 -0.55";
// When firing from a point offset from the eye, muzzle correction
// will adjust the muzzle vector to point to the eye LOS point.
// Since this weapon doesn't actually fire from the muzzle point,
// we need to turn this off.
correctMuzzleVector = false;
// Add the WeaponImage namespace as a parent, WeaponImage namespace
// provides some hooks into the inventory system.
className = "WeaponImage";
// Projectile && Ammo.
item = Rifle;
ammo = RifleAmmo;
projectile = RifleProjectile;
projectileType = Projectile;
casing = RifleShell;
// Images have a state system which controls how the animations
// are run, which sounds are played, script callbacks, etc. This
// state system is downloaded to the client so that clients can
// predict state changes and animate accordingly. The following
// system supports basic ready->fire->reload transitions as
// well as a no-ammo->dryfire idle state.
// Initial start up state
stateName[0] = "Preactivate";
stateTransitionOnLoaded[0] = "Activate";
stateTransitionOnNoAmmo[0] = "NoAmmo";
stateSound[0] = "RifleMountSound"; // **** Added in sound tutorial
// Activating the gun. Called when the weapon is first
// mounted and there is ammo.
stateName[1] = "Activate";
stateTransitionOnTimeout[1] = "Ready";
stateTimeoutValue[1] = 0.5;
stateSequence[1] = "Activate";
// Ready to fire, just waiting for the trigger
stateName[2] = "Ready";
stateTransitionOnNoAmmo[2] = "NoAmmo";
stateTransitionOnTriggerDown[2] = "Fire";
// Fire the weapon. Calls the fire script which does
// the actual work.
stateName[3] = "Fire";
stateTransitionOnTimeout[3] = "Reload";
stateTimeoutValue[3] = 0.1;
stateFire[3] = true;
stateRecoil[3] = LightRecoil;
stateAllowImageChange[3] = false;
stateSequence[3] = "Fire";
stateScript[3] = "onFire";
stateEmitter[3] = RifleFireEmitter;
stateEmitterTime[3] = 0.3;
stateSound[3] = "RifleFireSound"; // **** Added in sound tutorial
// Play the relead animation, and transition into
stateName[4] = "Reload";
stateTransitionOnNoAmmo[4] = "NoAmmo";
stateTransitionOnTimeout[4] = "Ready";
stateTimeoutValue[4] = 0.1;
stateAllowImageChange[4] = false;
stateSequence[4] = "Reload";
stateEjectShell[4] = true;
// No ammo in the weapon, just idle until something
// shows up. Play the dry fire sound if the trigger is
// pulled.
stateName[5] = "NoAmmo";
stateTransitionOnAmmo[5] = "Reload";
stateSequence[5] = "NoAmmo";
stateTransitionOnTriggerDown[5] = "DryFire";
// No ammo dry fire
stateName[6] = "DryFire";
stateTimeoutValue[6] = 1.0;
stateTransitionOnTimeout[6] = "NoAmmo";
stateSound[6] = "RifleDryFireSound"; // **** Added in sound tutorial
};
Basically what happens is when state 6 becomes the active state (ie. dryfire),
then the declared statesound is played. Really quite simple huh? :)
This is the first tutorial I have written but it is something that I believe a lot of
developers are having trouble with or would find useful so, I decided to get off my
slack ass and right a little tutorial for it. Hope it helps someone.
This tutorial does not require any engine modifications whatsoever and only
alters the game script files. In saying this, it should easily work with all versions
of the torque engine.
In this tutorial, any instructions that require editing will be prefixed with ****
WEAPON IMAGE SOUNDS
I will not go into sound effects in this tutorial but will rather concentrate on
the making of sounds in a weapon image. You can edit autoprofiles.cs and make
whatever distances you feel fit in there :)
Firstly, lets add a couple of sounds into the game
**** Open the file example/fps/server/scripts/rifle.cs
**** In or near the top of the file add the following lines
datablock AudioProfile(RifleMountSound)
{
filename = "~/data/sound/buttonOver.wav";
description = AudioClosest3d;
preload = true;
};
datablock AudioProfile(RifleFireSound)
{
filename = "~/data/sound/testing.wav";
description = AudioDefault3d;
preload = true;
};
datablock AudioProfile(RifleDryFireSound)
{
filename = "~/data/sound/footfall.wav";
description = AudioClose3d;
preload = true;
};
It is very important that these datablocks are defined BEFORE you define your weapon
image otherwise an error will be sent to the console and it simply wont work
**** Find the RifleImage datablock. The changes I have made are followed by // **** Added in sound tutorial
datablock ShapeBaseImageData(RifleImage)
{
// Basic Item properties
shapeFile = "~/data/shapes/rifle/weapon.dts";
emap = true;
// Specify mount point & offset for 3rd person, and eye offset
// for first person rendering.
mountPoint = 0;
offset = "0 0 0";
eyeOffset = "0.1 0.2 -0.55";
// When firing from a point offset from the eye, muzzle correction
// will adjust the muzzle vector to point to the eye LOS point.
// Since this weapon doesn't actually fire from the muzzle point,
// we need to turn this off.
correctMuzzleVector = false;
// Add the WeaponImage namespace as a parent, WeaponImage namespace
// provides some hooks into the inventory system.
className = "WeaponImage";
// Projectile && Ammo.
item = Rifle;
ammo = RifleAmmo;
projectile = RifleProjectile;
projectileType = Projectile;
casing = RifleShell;
// Images have a state system which controls how the animations
// are run, which sounds are played, script callbacks, etc. This
// state system is downloaded to the client so that clients can
// predict state changes and animate accordingly. The following
// system supports basic ready->fire->reload transitions as
// well as a no-ammo->dryfire idle state.
// Initial start up state
stateName[0] = "Preactivate";
stateTransitionOnLoaded[0] = "Activate";
stateTransitionOnNoAmmo[0] = "NoAmmo";
stateSound[0] = "RifleMountSound"; // **** Added in sound tutorial
// Activating the gun. Called when the weapon is first
// mounted and there is ammo.
stateName[1] = "Activate";
stateTransitionOnTimeout[1] = "Ready";
stateTimeoutValue[1] = 0.5;
stateSequence[1] = "Activate";
// Ready to fire, just waiting for the trigger
stateName[2] = "Ready";
stateTransitionOnNoAmmo[2] = "NoAmmo";
stateTransitionOnTriggerDown[2] = "Fire";
// Fire the weapon. Calls the fire script which does
// the actual work.
stateName[3] = "Fire";
stateTransitionOnTimeout[3] = "Reload";
stateTimeoutValue[3] = 0.1;
stateFire[3] = true;
stateRecoil[3] = LightRecoil;
stateAllowImageChange[3] = false;
stateSequence[3] = "Fire";
stateScript[3] = "onFire";
stateEmitter[3] = RifleFireEmitter;
stateEmitterTime[3] = 0.3;
stateSound[3] = "RifleFireSound"; // **** Added in sound tutorial
// Play the relead animation, and transition into
stateName[4] = "Reload";
stateTransitionOnNoAmmo[4] = "NoAmmo";
stateTransitionOnTimeout[4] = "Ready";
stateTimeoutValue[4] = 0.1;
stateAllowImageChange[4] = false;
stateSequence[4] = "Reload";
stateEjectShell[4] = true;
// No ammo in the weapon, just idle until something
// shows up. Play the dry fire sound if the trigger is
// pulled.
stateName[5] = "NoAmmo";
stateTransitionOnAmmo[5] = "Reload";
stateSequence[5] = "NoAmmo";
stateTransitionOnTriggerDown[5] = "DryFire";
// No ammo dry fire
stateName[6] = "DryFire";
stateTimeoutValue[6] = 1.0;
stateTransitionOnTimeout[6] = "NoAmmo";
stateSound[6] = "RifleDryFireSound"; // **** Added in sound tutorial
};
Basically what happens is when state 6 becomes the active state (ie. dryfire),
then the declared statesound is played. Really quite simple huh? :)
About the author
#2
The code alterations would have a little easier to spot if they had been in bold, but that is a minor criticism.
The only problem I had with it was that I found that the sounds were really quiet unless I moved while they were playing. I am guessing that is because they are 3d because when I changed them to Audio2D they were nice and loud.
If the sound is Audio2D, does that mean that other clients cannot hear the sound?
03/09/2002 (2:28 pm)
Great tutorial!The code alterations would have a little easier to spot if they had been in bold, but that is a minor criticism.
The only problem I had with it was that I found that the sounds were really quiet unless I moved while they were playing. I am guessing that is because they are 3d because when I changed them to Audio2D they were nice and loud.
If the sound is Audio2D, does that mean that other clients cannot hear the sound?
#3
10/21/2002 (8:53 am)
2 out of 3 of the sounds play. The mount sound does not play. Any ideas?
#4
if u use a wav that the engine doesnt understand
it plays a different (why o why ?) or no sound at all.
Searched the whole day for that...
02/02/2004 (4:13 am)
Just a quick note, as of Release 1_2_0 and current headif u use a wav that the engine doesnt understand
it plays a different (why o why ?) or no sound at all.
Searched the whole day for that...
#5
... it was playing sounds from another weapon... i converted the sound to an ogg using audacity and it worked perfect.
07/17/2004 (9:03 pm)
i had the problem...... it was playing sounds from another weapon... i converted the sound to an ogg using audacity and it worked perfect.
#6
datablock AudioProfile(BlowUpSound)
{
filename = "~/data/sound/testing.wav";
description = AudioDefault3d;
preload = true;
};
||||||
drop that vvvvvv into the ---> datablock ExplosionData <----
soundProfile = BlowUpSoundSound;
and it should play an explosion sound
07/17/2004 (9:05 pm)
oh ya ... another thing... if you set up an explosion sound datablock AudioProfile(BlowUpSound)
{
filename = "~/data/sound/testing.wav";
description = AudioDefault3d;
preload = true;
};
||||||
drop that vvvvvv into the ---> datablock ExplosionData <----
soundProfile = BlowUpSoundSound;
and it should play an explosion sound

Torque Owner Ace