Weapon Looping Sound Problems HELP!!!
by Tim Heldna · in Torque Game Engine · 04/06/2005 (6:08 am) · 16 replies
Hi everyone,
I'm hoping to gain a little help here because i'm stumped on this one and getting really frustrated...
My problem is that on machine gun type weapons with a high rate of fire the sound isn't played correctly. IE It fires erratically like it's missing a beat every 4th or 5th shot.
On searching the forums i found other people were having this problem and one suggestion was to use a looping sound for the gun fire sound, set to loop on trigger down, then set a trail / after shot sound on trigger up.
This works fine as far as the sound goes but i found that once i altered my weapon script to accomadate this setup a few things stopped working.
The weapon light is only displayed when the mouse button is initially pressed but doesn't continue to loop as it is held down. This is the same for the smoke emitter that comes out of the nozzle and also the player arm recoil animation.
It seems when you take out the refire function, which i found necessary as far as sound goes, you loose the above three functions.
Is there any way around this? All i can think of is putting something in the
function 'your weapon name' Image::onFire(%this, %obj, %slot) section of script that plays the above three items (weaponlight / smoke emitter / arm recoil) in the same way that the projectile is generated. I don't even know if this is possible or how to do it if it is. Other than that i'm guessing that something has to be done in shapebase.cc that calls on these functions on triggerdown as opposed to statefire.
As you probably guessed im new to scripting so heres an example to explain that last point a little better.
In shapebase.cc at line 3096 it has the following:
Im guessing (as i mentioned earlier i'm new to scripting so tell me if im wrong) that basically what this does is declare that if fire state = true then display the weapon light. If i'm not wrong, and i realise i probably am, i figure you could alter this section of script to declare that if the mouse button is down then play the weapon light.
I hope all this is clear, if anyone can help out then please do so, this is really holding up our game.
I'm hoping to gain a little help here because i'm stumped on this one and getting really frustrated...
My problem is that on machine gun type weapons with a high rate of fire the sound isn't played correctly. IE It fires erratically like it's missing a beat every 4th or 5th shot.
On searching the forums i found other people were having this problem and one suggestion was to use a looping sound for the gun fire sound, set to loop on trigger down, then set a trail / after shot sound on trigger up.
This works fine as far as the sound goes but i found that once i altered my weapon script to accomadate this setup a few things stopped working.
The weapon light is only displayed when the mouse button is initially pressed but doesn't continue to loop as it is held down. This is the same for the smoke emitter that comes out of the nozzle and also the player arm recoil animation.
It seems when you take out the refire function, which i found necessary as far as sound goes, you loose the above three functions.
Is there any way around this? All i can think of is putting something in the
function 'your weapon name' Image::onFire(%this, %obj, %slot) section of script that plays the above three items (weaponlight / smoke emitter / arm recoil) in the same way that the projectile is generated. I don't even know if this is possible or how to do it if it is. Other than that i'm guessing that something has to be done in shapebase.cc that calls on these functions on triggerdown as opposed to statefire.
As you probably guessed im new to scripting so heres an example to explain that last point a little better.
In shapebase.cc at line 3096 it has the following:
if (isProperlyAdded()) {
// Normal processing
if (count != image.fireCount)
{
image.fireCount = count;
setImageState(i,getImageFireState(i),true);
if( imageData && imageData->lightType == ShapeBaseImageData::WeaponFireLight )
{
mLightTime = Sim::getCurrentTime();
}
}
updateImageState(i,0);
}Im guessing (as i mentioned earlier i'm new to scripting so tell me if im wrong) that basically what this does is declare that if fire state = true then display the weapon light. If i'm not wrong, and i realise i probably am, i figure you could alter this section of script to declare that if the mouse button is down then play the weapon light.
I hope all this is clear, if anyone can help out then please do so, this is really holding up our game.
About the author
#2
04/06/2005 (6:26 am)
Did you search the forums... cause I remember a similair topic a little while back
#3
Yeah i reckon i've read every forum post on this but haven't found an answer as yet.
04/06/2005 (6:41 am)
Chris,Yeah i reckon i've read every forum post on this but haven't found an answer as yet.
#4
04/06/2005 (6:53 am)
Easiest solution, is to get Tribes 2 and see how they implimented the chaingun.
#5
04/06/2005 (6:55 am)
Anthony are you sure that will work? There's alot of unknown stuff in there that doesn't seem to work with TGE these days. For instance the spinup/spindown animations.
#6
I believe the problem lies in your individual weapon script, i think it's the ReFire function which is causing things to stuff up. I say this cos not only the sound plays erratically but also the recoil of the gun.
To understand what i'm on about download the zip file from this resource www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6221 and put the machine gun in your game and you'll see what i mean. Then look at the script for the machine gun, paying attention to the 'fire' and 'refire' sections and you should start to get an idea of what i'm on about.
To ellaborate, using the machine gun.dts file and machine gun script from the above posted resource it's not only the sound that plays erratically, but everything else such as recoils, gun flashes etc. From my own testing i have confirmed that this is caused by the 'ReFire' function and am seeking an alternate way to script a machine gun with a looping sound (to fix the sound issue which i don't believe is related to sound channels filling up as some people have suggested) without loosing recoil and weapon light effects.
Well i've dribbled on enough, hopes this makes sense. If you still don't believe me download beaver patrol and fire the mp5 or ak47 and hear for yourself how crap it sounds when they don't fire evenly.
04/06/2005 (7:18 am)
I've got tribes 2 and the chaingun script but they don't have a recoil, weapon light or emitter on it. They only have a spin up and spin down animation. For effects that are part of the actual dts model, like recoil in 1st person view and a gun flash there's no problem cos you can set these things to loop in milkshape. But for things like the weapon light and smoke emitter they are set in torque and that's where my problem lies. I put this sound upgrade in from the following resource www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6863 which did improve overall sound quality but didn't solve the skipping sound problem on weapons with a high rate of fire using a single shot unlooping sound file. I also made this change in ShapeImage.cc - c:\Torque\Engine\Game The TGE engine uses a looped audio sound which, for fast firing weapons was causing all of the audio channels to fill up. I reworked this code to use one audio channel only and use singular (non-looped) sounds. The result is a much cleaner and clearer sounding weapon.
Lines at approx 1616 look like this
// Play sound
if (stateData.sound && isGhost()) {
Point3F vel = getVelocity();
image.animSound = alxPlay(stateData.sound, &getRenderTransform(), &vel);
ALint value = 0;
alxGetSourcei(image.animSound, AL_LOOPING, &value);
image.animLoopingSound = (value == AL_TRUE);
}
Replace with this
// Play sound
if (stateData.sound && isGhost()) {
Point3F vel = getVelocity();
if(image.animSound)
{
alxStop(image.animSound);
image.animSound = 0;
}
image.animSound = alxPlay(stateData.sound, &getRenderTransform(), &vel);
ALint value = 0;
//alxGetSourcei(image.animSound, AL_LOOPING, &value);
image.animLoopingSound = false;//(value == AL_TRUE);
}
What this does is plays a sound and, if that sound is still playign when the next firing sound is played, it stops it before playing the next sound. This means you can use a nice long 2 second rifle sound in an automatic rifle (if you wish) and the last firing sound will have that nice long fade. but once again it only slightly improved the problem. I believe the problem lies in your individual weapon script, i think it's the ReFire function which is causing things to stuff up. I say this cos not only the sound plays erratically but also the recoil of the gun.
To understand what i'm on about download the zip file from this resource www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6221 and put the machine gun in your game and you'll see what i mean. Then look at the script for the machine gun, paying attention to the 'fire' and 'refire' sections and you should start to get an idea of what i'm on about.
To ellaborate, using the machine gun.dts file and machine gun script from the above posted resource it's not only the sound that plays erratically, but everything else such as recoils, gun flashes etc. From my own testing i have confirmed that this is caused by the 'ReFire' function and am seeking an alternate way to script a machine gun with a looping sound (to fix the sound issue which i don't believe is related to sound channels filling up as some people have suggested) without loosing recoil and weapon light effects.
Well i've dribbled on enough, hopes this makes sense. If you still don't believe me download beaver patrol and fire the mp5 or ak47 and hear for yourself how crap it sounds when they don't fire evenly.
#7
04/06/2005 (8:13 am)
@Stefan it does work, I had to use it as inspiration for Lore's gatlins
#8
Anthony,
Mimicking the Tribes 2 chaingun script works fine but as i mentioned earlier they haven't a smoke emitter or weapon fire light on this gun so it's not much help.
What i need to do is have these events (smoke emitter, weapon fire light, 3rd person arm recoil) be triggered on mouse button down, continue to loop whilst held down, then stop on mouse button up.
Can someone please help me in implementing this, please please please please please :)
There's not many first person shoot em ups on Torque at the moment. Of the few i've played this problem still exists and i just don't think it's acceptable. The weapon, in a first person shoot em up, is the main thing on screen and should look and sound the part in all regards.
If anyone else has overcome this obstacle please let me know.
04/08/2005 (11:26 pm)
Quote:Easiest solution, is to get Tribes 2 and see how they implimented the chaingun.
Anthony,
Mimicking the Tribes 2 chaingun script works fine but as i mentioned earlier they haven't a smoke emitter or weapon fire light on this gun so it's not much help.
What i need to do is have these events (smoke emitter, weapon fire light, 3rd person arm recoil) be triggered on mouse button down, continue to loop whilst held down, then stop on mouse button up.
Can someone please help me in implementing this, please please please please please :)
There's not many first person shoot em ups on Torque at the moment. Of the few i've played this problem still exists and i just don't think it's acceptable. The weapon, in a first person shoot em up, is the main thing on screen and should look and sound the part in all regards.
If anyone else has overcome this obstacle please let me know.
#9
Here's the code for the state machine:
At the top of my onFire function, I check if the sound is being played and if it isn't, then play it:
The funciton onEndFire just checks if the sound is playing and if it is, then stop it and play the sound for the weapon falloff.
I also made the changes to the source that were mentioned earlier to help in case somebody presses the fire button too quickly. Just be sure the sound bite you use for the shot is as long as your firing cycle. Otherwise, you'll end up with some wierd effects. I hope this helps you out. I know it's a few months late, but I had this issue as well and I figure somebody else might in the future and figured it'd be a great thing to contribute to the community. Also, if there is a more efficient way of doing this, please let me know. I'm fairly new to Torque and I don't know the performance of some of the features very well.
07/15/2005 (5:26 pm)
I was recently posed with this issue as well and I was able to come up with a solution that works well for me. The weapon I'm using has infinite ammo, but other than that, it's works well as a machine gun. The weapon has muzzle emitters for the guns to make it appear smoke is coming out of them. I'm sure adding the ammo states and allowing for other effects wouldn't be much difficult.Here's the code for the state machine:
// Initial start up state stateName[0] = "Preactivate"; stateTransitionOnLoaded[0] = "Activate"; stateTransitionOnNoAmmo[0] = "Activate"; // Activating the gun. Called when the weapon is first // mounted and there is ammo. stateName[1] = "Activate"; stateTransitionOnTimeout[1] = "Ready"; stateTimeoutValue[1] = 0.001; stateSequence[1] = "Activate"; // Ready to fire, just waiting for the trigger stateName[2] = "Ready"; stateTransitionOnTriggerDown[2] = "Fire"; // Fire the weapon. Calls the fire script which does // the actual work. stateName[3] = "Fire"; stateTransitionOnTimeout[3] = "Reload"; stateTimeoutValue[3] = 0.02; stateFire[3] = true; stateRecoil[3] = LightRecoil; stateAllowImageChange[3] = false; stateSequence[3] = "Fire"; stateScript[3] = "onFire"; stateEmitter[3] = MSmokeEmitter; stateEmitterTime[3] = 0.1; // Play the relead animation, and transition into stateName[4] = "Reload"; stateTransitionOnTimeout[4] = "CheckReady"; stateTimeoutValue[4] = 0.1; stateAllowImageChange[4] = false; stateSequence[4] = "Reload"; stateEjectShell[4] = true; // call the function to end the machine gun sound stateName[5] = "EndFire"; stateScript[5] = "onEndFire"; stateTransitionOnTimeout[5] = "Ready"; stateTimeoutValue[5] = 0.0; // check if the gun is still being fired stateName[6] = "CheckReady"; stateTransitionOnTriggerDown[6] = "Ready"; stateTransitionOnTriggerUp[6] = "EndFire";
At the top of my onFire function, I check if the sound is being played and if it isn't, then play it:
if (!alxIsPlaying($MachineGunSndHandle))
{
$MachineGunSndHandle = alxCreateSource("AudioCloseLooping3d", expandFilename("~/data/sound/gun_dual_rifle1.wav"));
alxPlay($MachineGunSndHandle);
}The funciton onEndFire just checks if the sound is playing and if it is, then stop it and play the sound for the weapon falloff.
function ShapeBaseImageData::onEndFire(%data, %obj, %slot)
{
if (alxIsPlaying($MachineGunSndHandle))
{
alxStop($MachineGunSndHandle);
%MachineGunFallSndHandle = alxCreateSource("AudioClose3d", expandFilename("~/data/sound/gun_dual_rifle2.wav"));
alxPlay(%MachineGunFallSndHandle);
}
}I also made the changes to the source that were mentioned earlier to help in case somebody presses the fire button too quickly. Just be sure the sound bite you use for the shot is as long as your firing cycle. Otherwise, you'll end up with some wierd effects. I hope this helps you out. I know it's a few months late, but I had this issue as well and I figure somebody else might in the future and figured it'd be a great thing to contribute to the community. Also, if there is a more efficient way of doing this, please let me know. I'm fairly new to Torque and I don't know the performance of some of the features very well.
#10
Thanks for the response though.
12/07/2005 (9:13 pm)
Very interseting solution. I did manage to fix this another way by digging through the old tribes2 scripts and copying some stuff over.Thanks for the response though.
#11
*What* in the Tribes2 scripts were the solution?
01/11/2006 (7:55 am)
Tim, after so many posts in this thread and people trying to help you, wouldn't it be nice if you actually told us what you did to solve the problem rather than just leaving it unsolved with a clue?*What* in the Tribes2 scripts were the solution?
#12
As you can see i made a few tweaks, also make sure the chaingun fire sound is set to looping so you get that nice even firing sound.
01/26/2006 (9:54 pm)
Sorry Stefan, i didn't realise anyone else was interested. My apologies. All i did was use the tribes 2 chaingun script as suggested by Anthony Rosenbaum. Here it is...stateName[0] = "Activate"; stateSequence[0] = "Activate"; stateSound[0] = ChaingunSwitchSound; stateAllowImageChange[0] = false; // stateTimeoutValue[0] = 0.5; stateTransitionOnTimeout[0] = "Ready"; stateTransitionOnNoAmmo[0] = "NoAmmo"; //-------------------------------------- stateName[1] = "Ready"; stateSpinThread[1] = Stop; // stateTransitionOnTriggerDown[1] = "Spinup"; stateTransitionOnNoAmmo[1] = "NoAmmo"; //-------------------------------------- stateName[2] = "NoAmmo"; stateTransitionOnAmmo[2] = "Ready"; stateSpinThread[2] = Stop; stateTransitionOnTriggerDown[2] = "DryFire"; //-------------------------------------- stateName[3] = "Spinup"; stateSpinThread[3] = SpinUp; stateSound[3] = ChaingunSpinupSound; // stateTimeoutValue[3] = 0.5; stateWaitForTimeout[3] = false; stateTransitionOnTimeout[3] = "Fire"; stateEmitter[3] = ChaingunFireEmitter;//If emitter here then it will play once at start of fire stateEmitterTime[3] = 0.12; //How long emitter lasts stateTransitionOnTriggerUp[3] = "Spindown"; //-------------------------------------- stateName[4] = "PreFire";//This is a hack to make sure the weapon light continues to loop stateTransitionOnTriggerDown[4] = "Fire"; //whilst the mouse button is held down. //-------------------------------------- stateName[5] = "Fire"; stateSequence[5] = "Fire"; stateSequenceRandomFlash[5] = true; stateSpinThread[5] = FullSpeed; stateSound[5] = ChaingunFireSound; //stateRecoil[5] = LightRecoil; stateAllowImageChange[5] = false; stateScript[5] = "onFire"; stateFire[5] = true; // //stateEmitter[5] = ChaingunFireEmitter; //If emitter here then it will loop continueously //stateEmitterTime[5] = 0.12; //How long emitter lasts stateEjectShell[5] = true; // stateTimeoutValue[5] = 0.15; stateTransitionOnTimeout[5] = "PreFire";//Originally "Fire"; refer hack note above stateTransitionOnTriggerUp[5] = "Spindown"; stateTransitionOnNoAmmo[5] = "EmptySpindown"; //-------------------------------------- stateName[6] = "Spindown"; stateSound[6] = ChaingunSpinDownSound; stateSpinThread[6] = SpinDown; // stateEmitter[6] = ChaingunImpactSmoke; //If emitter here then it will play at end of fire stateEmitterTime[6] = 0.12; //How long emitter lasts stateTimeoutValue[6] = 1.0; stateWaitForTimeout[6] = true; stateTransitionOnTimeout[6] = "Ready"; stateTransitionOnTriggerDown[6] = "Spinup"; //-------------------------------------- stateName[7] = "EmptySpindown"; stateSound[7] = ChaingunSpinDownSound; stateSpinThread[7] = SpinDown; // stateTimeoutValue[7] = 0.5; stateTransitionOnTimeout[7] = "NoAmmo"; //-------------------------------------- stateName[8] = "DryFire"; stateSound[8] = ChaingunDryFireSound; stateTimeoutValue[8] = 0.5; stateTransitionOnTimeout[8] = "NoAmmo"; };
As you can see i made a few tweaks, also make sure the chaingun fire sound is set to looping so you get that nice even firing sound.
#13
Like.. dit dit dit ... dit dit dit dit .... dit dit dit. Not smoothly.
Sure I'll end up using a looping sound, but I felt like reporting. :)
02/20/2006 (10:36 am)
(A little late) I've found that not only the sound skips a beat every 3 or 4 shots, but so does the gun animation and firing. Like.. dit dit dit ... dit dit dit dit .... dit dit dit. Not smoothly.
Sure I'll end up using a looping sound, but I felt like reporting. :)
#14
You're supposed to be using a looping sound for the above script
02/20/2006 (7:39 pm)
Strange, I used the above script for a minigun with no problems whatsover. You have to have all the animations setup correctly within the dts model ie spinup, spindown etc.Quote:
Sure I'll end up using a looping sound, but I felt like reporting.
You're supposed to be using a looping sound for the above script
#15
2 Chainguns mounted on Helicopter (2.5mb)
Chaingun mounted on player (1.11mb)
02/20/2006 (8:58 pm)
They say a picture is worth 1000 words, well here's a video...2 Chainguns mounted on Helicopter (2.5mb)
Chaingun mounted on player (1.11mb)
#16
03/02/2006 (1:10 pm)
Actually I was just commenting on the non-looping animation/sound system. I'll use this eventually.
Torque Owner Tim Heldna