Page 1 of 1

Shot Counter

Posted: December 8th, 2013, 11:37 am
by csocsi96
Hi Everybody,

I am asking the community for some help if possible.

I am working on a script which counts the number of fires that you shoot and stores into a variable.

The problem is that when I press the attack button and shoot a few times, it counts a tons of shots, for exmaple if I shoot 3 times it counts 18260520 etc... so I guess the script is running without pauses?

The code i am using:

Code: Select all

init() {
   
    level waittill("connected", player);
    player waittill("spawned");
    player thread monitorWeaponUsage();
}

monitorWeaponUsage() {
    ammo_before_wait = 0;
    ammo_after_wait = 0;
    
    while(isAlive(self)) {
        if(self attackButtonPressed()) {
            fired = true;
			ammo_after_wait = getCurrentWeaponClipAmmo(self);
        } else {
            fired = false;
			ammo_before_wait = getCurrentWeaponClipAmmo(self);
        }
        
        if(fired) {
			difference = ammo_before_wait - ammo_after_wait; 
					self.firecount += difference;
                  //  self iPrintLnBold("You Fired.");
        } else {
            //Avoid infinite loop
            wait 0.05;
        }
    }
}

getCurrentWeaponClipAmmo(currPlayer) {
   
    if(currPlayer getWeaponSlotWeapon("primary") == currPlayer getCurrentWeapon()) {
        weapon = "primary";
    } else {
        weapon = "primaryb";
    }

   ammo = currPlayer getWeaponSlotClipAmmo(weapon);

    //Returns with the current value in the clip.
    return ammo;
}
I'd really appreciate your help.

Regards,
Unrealxqt

Re: Shot Counter

Posted: December 9th, 2013, 12:10 am
by Drofder2004
As you suspect, you are not waiting.

You need to separate the calculations from the loop and only calculate at two points:
1. When person stops shooting
2. When person is holding attack but is reloading (special circumstance)

Code: Select all

init() {
   level waittill("connected", player);
   player waittill("spawned");
   player thread monitorWeaponUsage();
}
 
monitorWeaponUsage() {
   ammo_before_wait = 0;
   ammo_after_wait = 0;
   
   while(isAlive(self)) {
      /* wait for player to click attack button */
      while(!self attackButtonPressed()){
         wait 0.05;
      }
 
      /* set the ammo_before_wait to current ammo */
      ammo_before_wait = getCurrentWeaponClipAmmo(self);
 
      /* special circumstance: if the player does not let go of attack, and auto-reloads */
      while(self attackButtonPressed()) {
         wait 0.05;
 
         /* Check if clip is empty */
         if(getCurrentWeaponClipAmmo(self) == 0) {
            self.firecount += ammo_before_wait;
 
            /* set ammo_before_wait to 0, if the player stops attacking while reloading, 
            this avoids a double count */
            ammo_before_wait = 0;
 
            /* wait while reloading */
            while(getCurrentWeaponClipAmmo(self) == 0) {
               wait 0.05;
            }
 
            /* reset ammo_before_wait */
            ammo_before_wait = getCurrentWeaponClipAmmo(self);
         }
      }
 
      /* Player has stopped attacking, calculate shots fired */
      self.firecount += ammo_before_wait - getCurrentWeaponClipAmmo(self);
   }
}
I have not tested the above code however the approach I have shown is the way I would do it.
However, I must add that I highly doubt it is possible to count every bullet, as built in engine code can calculate accurately, we can only calculate 1/20th of a second. That small brief window may cause a miscount.

Re: Shot Counter

Posted: December 9th, 2013, 8:05 am
by Nekoneko
simple as this

Code: Select all

 
 
init()
{
   
    level waittill("connected", player);
    player thread monitorWeaponUsage();
}
 
 
monitorWeaponUsage();
{
        self endon( "disconnect" );
        
        self.firecount = 0;
        
        for ( ;; )
        {       
                self waittill ( "weapon_fired" );
                self.firecount++;
        }
}

Re: Shot Counter

Posted: December 9th, 2013, 3:22 pm
by Drofder2004
Its been so many years since I have modded, and I only just learned this is a waittill event? O_o

Re: Shot Counter

Posted: December 9th, 2013, 5:12 pm
by csocsi96
Thank you guys for the answers! I really appreciate it! :-)

Re: Shot Counter

Posted: December 17th, 2013, 7:10 am
by Nekoneko
Drofder2004 wrote:Its been so many years since I have modded, and I only just learned this is a waittill event? O_o
always learn something new, don't ya :)

Re: Shot Counter

Posted: December 17th, 2013, 9:48 pm
by Drofder2004
Nekoneko wrote:
Drofder2004 wrote:Its been so many years since I have modded, and I only just learned this is a waittill event? O_o
always learn something new, don't ya :)
Absolutely, not that I will ever make use of it :P