Author Topic: Using a loop as a gun sound (Help)  (Read 1890 times)

So i have this Loop of an M416 that i have attached to the forum in case you want to listen to it. 30 rounds at 750 rpm, it loops perfectly.

What i want to do is make it so when i fire my weapon it plays a looped sound instead of a single sound being repeated. Obviously im going to need to give the sound a datablock that lets it loop, im going to use AudioDefaultLooping3D

How would i implement this into my weapon?

I actually did this with one of my guns, and I don't think I put in a looping datablock for it, but when it stops firing, the sound stops playing

I actually did this with one of my guns, and I don't think I put in a looping datablock for it, but when it stops firing, the sound stops playing
could you explain and or show me how you did that?

I don't really know anything about torquescript specifically but I think the most obvious thing is to play the sound based on when the trigger is being held rather than based on the gun firing. that means that when the button is released, if the sound is currently in the middle of a gunshot, it'll cut off in a very odd way that probably nobody will like
the RYNO-V does this, which can be found here, if you're looking for a specific example

but... given the way the sound will have to end... why do you want to do it this way? I honestly can't think of any advantages. as for downsides: you had to ask how to do it, it won't sound good, AND it's gonna add a lot of bulk to the zip file

could you explain and or show me how you did that?
it's kind of what i said, it plays the loop when you're firing, but when you stop, it stops

so idk

could you explain and or show me how you did that?
First, you put this around the bottom of your item's main CS file
Code: [Select]
function ITEM_NAMEImage::onFire(%this, %obj, %slot)
{
%obj.playAudio(0, AUDIO_NAMESound);
Parent::onFire(%this,%obj,%slot);
}
function ITEM_NAMEImage::onStopFire(%this, %obj, %slot)
{
%obj.stopAudio(0);
}

Then make sure that your weapon has a 'StopFire' state and that it has a 'stateScript' entry, it looks like this
Code: [Select]
stateName[#] = "StopFire";
stateScript[#]                  = "onStopFire";
stateTimeoutValue[#]            = 0.5;
stateTransitionOnTimeout[#]     = "Reload";

As for the audio datablock referenced in the onFire script, it should look like this and be around the top of your main CS file
Code: [Select]
datablock AudioProfile(AUDIO_NAMESound)
{
   filename    = "./Your_Audio_File.wav";
   description = AudioClosestLooping3d;
   preload = true;
};

-snip-

Ah thank you, ill implement this and decide whether or not it is worth it.

I don't really know anything about torquescript specifically but I think the most obvious thing is to play the sound based on when the trigger is being held rather than based on the gun firing. that means that when the button is released, if the sound is currently in the middle of a gunshot, it'll cut off in a very odd way that probably nobody will like
the RYNO-V does this, which can be found here, if you're looking for a specific example

but... given the way the sound will have to end... why do you want to do it this way? I honestly can't think of any advantages. as for downsides: you had to ask how to do it, it won't sound good, AND it's gonna add a lot of bulk to the zip file
I have heeded your warning, but i learn best from mistakes, and if this doesnt go well, at least you told me so.

Ran into some trouble. The sound wont cut iff when i release the trigger.

Datablock
Code: [Select]
datablock AudioProfile(M4Fire)
{
   filename    = "./Sounds/M4Fire.wav";
   description = AudioDefaultLooping3d;
   preload = true;
};

State Sequence
Code: [Select]
stateName[0]                    = "Activate";
stateTimeoutValue[0]            = 0.15;
stateTransitionOnTimeout[0]     = "LoadCheckA";

stateName[1]                    = "Ready";
stateTransitionOnNoAmmo[1] = "ReloadStart";
stateTransitionOnTriggerDown[1] = "Fire";
stateAllowImageChange[1]        = true;
stateSequence[1] = "idle";

stateName[2]                    = "Fire";
stateTransitionOnTimeout[2]     = "Smoke";
stateTransitionOnTriggerUp[2] = "Smoke";
stateWaitForTimeout[2] = true;
stateTimeoutValue[2]            = 0.00;
stateFire[2]                    = true;
stateAllowImageChange[2]        = false;
stateScript[2]                  = "onFire";
stateSequence[2] = "Fire";
stateEmitter[2] = testmuzzleFireEmitter;
stateEmitterTime[2] = 0.01;
stateEmitterNode[2] = "muzzleNode";
stateSound[2] = M4Fire;

stateName[3] = "Smoke";
stateTimeoutValue[3] = 0.035;
stateEmitter[3] = testSmokeEmitter;
stateEmitterTime[3] = 0.04;
stateEmitterNode[3] = "muzzleNode";
stateTransitionOnTimeout[3] = "Wait";
// stateTransitionOnTriggerDown[3] = "Wait";  remove timeout and triggerdown for semiauto

stateName[4] = "Wait";
stateEjectShell[2]              = true;
stateTimeoutValue[4] = 0.002;
stateScript[4]                  = "onBounce";
stateTransitionOnTimeout[4] = "LoadCheckA";

//Torque switches states instantly if there is an ammo/noammo state, regardless of stateWaitForTimeout
stateName[5] = "LoadCheckA";
stateScript[5] = "onLoadCheck";
stateTimeoutValue[5] = 0.00;
stateTransitionOnTimeout[5] = "LoadCheckB";

stateName[6] = "LoadCheckB";
stateTransitionOnAmmo[6] = "Ready";
stateTransitionOnNoAmmo[6] = "ReloadWait";

stateName[7] = "ReloadWait";
stateTimeoutValue[7] = 0.3;
stateScript[7] = "";
stateTransitionOnTimeout[7] = "ReloadStart";
stateWaitForTimeout[7] = true;

stateName[8] = "ReloadStart";
stateTimeoutValue[8] = 1.0;
stateScript[8] = "onReloadStart";
stateTransitionOnTimeout[8] = "Reloaded";
stateWaitForTimeout[8] = true;

stateName[9] = "Reloaded";
stateTimeoutValue[9] = 0.0;
stateScript[9] = "onReloaded";
stateTransitionOnTimeout[9] = "Ready";

stateName[10] = "StopFire";
stateScript[10]                  = "onStopFire";
stateTimeoutValue[10]            = 0.5;
stateTransitionOnTimeout[10]     = "Smoke";
};

Functions

Code: [Select]
function M4A1Image::onFire(%this,%obj,%slot)
{
%obj.playAudio(0, M4Fire);

%obj.spawnExplosion(SmallAutoRecoilProjectile,"1 1 1");
if(%obj.getDamagePercent() >= 1.0)
return;

if(vectorLen(%obj.getVelocity()) < 0.1 && (getSimTime() - %obj.lastShotTime) > 50)
{
%projectile = M4A1Projectile;
%spread = 0.007;
}
else
{
%projectile = M4A1Projectile;
%spread = 0.007;
}

%obj.lastShotTime = getSimTime();
%shellcount = 1;

%obj.playThread(2, Root);
%shellcount = 1;
%obj.toolAmmo[%obj.currTool]--;
commandToClient(%obj.client,'bottomPrint',"<color:0aa000><just:right><font:Arial:20>\c6" @ %obj.toolAmmo[%obj.currTool] @ " / " @ "31 " @ "", 4, 2, 3, 4);

for(%shell=0; %shell<%shellcount; %shell++)
{
%vector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%vector1 = VectorScale(%vector, %projectile.muzzleVelocity);
%vector2 = VectorScale(%objectVelocity, %projectile.velInheritFactor);
%velocity = VectorAdd(%vector1,%vector2);
%x = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%y = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%z = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
%velocity = MatrixMulVector(%mat, %velocity);

%p = new (%this.projectileType)()
{
dataBlock = %projectile;
initialVelocity = %velocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
MissionCleanup.add(%p);
}
return %p;
Parent::onFire(%this,%obj,%slot);
}
function M4A1Image::onStopFire(%this, %obj, %slot)
{
%obj.stopAudio(0);
}


Change stateTransitionOnTriggerUp in the Fire state to go to StopFire instead of Smoke

Change stateTransitionOnTriggerUp in the Fire state to go to StopFire instead of Smoke
Didnt do it, the sound still continues to loop.

Try this for one of your states in the image datablock

stateSound[number] = SoundDatablockName;

Yeah I did this for a fire staff I made once and I didn't have to do anything special. The sound should stop when the state is ended so it must be something wrong with your states. Make the [2], [5] and [9] states have a timeout value of 0.01, there might be problems with 0 as a time to be in the state.

stopAudio has slots, try using a different number than 0 (1-5)

stopAudio has slots, try using a different number than 0 (1-5)
'playAudio' has a sound playing in slot 0, so that doesn't need to be changed

None of your suggestions worked. Should i just scrap this idea?