chimueloeldragon2011 wrote:
Well, if you added the 'looping' key to the sound shader it'll surely loop. Otherwise I have no idea?
I've even added no_dups to make sure it does NOT loop. Also, i've moved it outside the While(1) loop, thinking that it was looping because of that. But nope.
Is the Idle state called again and again maybe? I don't think so.
EDIT: Full pistol script, if anyone wants to fix it for me.
Code:
#define PISTOL_FIRERATE 0.04
#define PISTOL_LOWAMMO 4
#define PISTOL_NUMPROJECTILES 1
// blend times
#define PISTOL_IDLE_TO_LOWER 2
#define PISTOL_IDLE_TO_FIRE 1
#define PISTOL_IDLE_TO_RELOAD 3
#define PISTOL_RAISE_TO_IDLE 3
#define PISTOL_FIRE_TO_IDLE 4
#define PISTOL_RELOAD_TO_IDLE 4
object weapon_pistol : weapon_base {
float next_attack;
float spread;
float cooldown;
float intensity;
float lightstate;
void init();
void Lower();
void Raise();
void Idle();
void Fire();
void Reload();
void ExitCinematic();
};
void weapon_pistol::init() {
next_attack = 0;
spread = getFloatKey( "spread" );
sys.setcvar("gunlight_state","0");
weaponState( "Raise", 0 );
}
void weapon_pistol::Raise() {
weaponRising();
playAnim( ANIMCHANNEL_ALL, "raise" );
waitUntil( animDone( ANIMCHANNEL_ALL, PISTOL_RAISE_TO_IDLE ) );
weaponState( "Idle", PISTOL_RAISE_TO_IDLE );
}
void weapon_pistol::Lower() {
weaponLowering();
playAnim( ANIMCHANNEL_ALL, "putaway" );
waitUntil( animDone( ANIMCHANNEL_ALL, 0 ) );
weaponHolstered();
waitUntil( WEAPON_RAISEWEAPON );
weaponState( "Raise", 0 );
}
void weapon_pistol::Idle() {
float currentTime;
float ammoClip;
float avail;
float clip_size;
float cooldown; //Needed only for lowering the spread(because i felt like connecting it to a float for better control)
clip_size = clipSize();
cooldown = 1; // set our new float to 1 so when the weapon is in the Idle state, spread decreases
lightstate = false;
sys.getcvar("gunlight_state");
weaponReady();
if (sys.getcvar("gunlight_state") == "1") {
startSoundShader("flashlight_switch",SND_CHANNEL_ITEM);
lightstate = true;
flashlight( lightstate );
}
if (sys.getcvar("gunlight_state") == "0") {
lightstate = false;
flashlight( lightstate );
}
if ( !ammoInClip() ) {
playCycle( ANIMCHANNEL_ALL, "idle_empty" );
} else {
playCycle( ANIMCHANNEL_ALL, "idle" );
}
while( 1 ) {
if ( WEAPON_LOWERWEAPON ) {
weaponState( "Lower", PISTOL_IDLE_TO_LOWER );
}
currentTime = sys.getTime();
ammoClip = ammoInClip();
if ( ( currentTime >= next_attack ) && WEAPON_ATTACK ) {
if ( ammoClip > 0 ) {
weaponState( "Fire", PISTOL_IDLE_TO_FIRE );
} else if ( ammoAvailable() > 0 ) {
if ( autoReload() ) {
cooldown = 1;
spread = 1;
netReload();
weaponState( "Reload", PISTOL_IDLE_TO_RELOAD );
}
}
}
if ( WEAPON_RELOAD && ( ammoAvailable() > ammoClip ) && ( ammoClip < clip_size ) ) {
cooldown = 1; //set it to 1 again just to be safe
spread = 1;
netReload();
weaponState( "Reload", PISTOL_IDLE_TO_RELOAD );
}
if (sys.getcvar("gunlight_state") == "1") {
startSoundShader("flashlight_switch",SND_CHANNEL_ITEM);
lightstate = true;
flashlight( lightstate );
}
if (sys.getcvar("gunlight_state") == "0") {
lightstate = false;
flashlight( lightstate );
}
if ( WEAPON_NETRELOAD ) {
spread = 1;
cooldown = 1; //...and again!
WEAPON_NETRELOAD = false;
weaponState( "Reload", PISTOL_IDLE_TO_RELOAD );
}
if ( cooldown == 1 ) { // the code itself. Note that adding a sys.wait will make reloading happen only 5 out of 10 times
spread = spread - 0.080; // this will loop numerous times...
}
if ( spread < 1 ) { // but spread can never go below 1.
spread = 1;
}
waitFrame();
}
}
void weapon_pistol::Fire() {
float ammoClip;
next_attack = sys.getTime() + PISTOL_FIRERATE;
ammoClip = ammoInClip();
if ( ammoClip == PISTOL_LOWAMMO ) {
startSound( "snd_lowammo", SND_CHANNEL_ITEM, true );
}
launchProjectiles( PISTOL_NUMPROJECTILES, spread, 0, 1.0, 1.0 );
startFx( "fx/muzzlelight.fx" );
playAnim( ANIMCHANNEL_ALL, "fire" );
if ( spread < 8 ) { // for some reason placing this code under the next_attack line results in only the first shot having increased spread.
spread = spread + 1.3; // this code loops as long as the mouse button is held due to the function being called over and over again.
}
waitUntil( animDone( ANIMCHANNEL_ALL, PISTOL_FIRE_TO_IDLE ) );
weaponState( "Idle", PISTOL_FIRE_TO_IDLE );
}
void weapon_pistol::Reload() {
weaponReloading();
playAnim( ANIMCHANNEL_ALL, "reload" );
waitUntil( animDone( ANIMCHANNEL_ALL, PISTOL_RELOAD_TO_IDLE ) );
addToClip( clipSize() );
weaponState( "Idle", PISTOL_RELOAD_TO_IDLE );
}
void weapon_pistol::ExitCinematic() {
next_attack = 0;
weaponState( "Idle", 0 );
}
I know it's messy. The toggle on & toggle off binds are at the beginning of the Idle state. No idea why the sound loops.
EDIT2: I'm stupid. Left the startSoundShader in the while loop. Will check if this is the source of the problem.
EDIT3: Yes, this was the problem. However, now i can't toggle my light without firing the weapon first. Weird.