Shot Counter

Have a question about modding, modelling or skinning? Have a tutorial to post? Post here!

Moderator: Core Staff

Post Reply
csocsi96
CJ Wannabe
CJ Wannabe
Posts: 3
Joined: December 8th, 2013, 11:26 am

Shot Counter

Post by csocsi96 » December 8th, 2013, 11:37 am

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

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Shot Counter

Post by Drofder2004 » December 9th, 2013, 12:10 am

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.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

Nekoneko
CJ Fan
CJ Fan
Posts: 170
Joined: April 18th, 2011, 3:48 pm

Re: Shot Counter

Post by Nekoneko » December 9th, 2013, 8:05 am

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++;
        }
}

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Shot Counter

Post by Drofder2004 » December 9th, 2013, 3:22 pm

Its been so many years since I have modded, and I only just learned this is a waittill event? O_o
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

csocsi96
CJ Wannabe
CJ Wannabe
Posts: 3
Joined: December 8th, 2013, 11:26 am

Re: Shot Counter

Post by csocsi96 » December 9th, 2013, 5:12 pm

Thank you guys for the answers! I really appreciate it! :-)

Nekoneko
CJ Fan
CJ Fan
Posts: 170
Joined: April 18th, 2011, 3:48 pm

Re: Shot Counter

Post by Nekoneko » December 17th, 2013, 7:10 am

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 :)

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: Shot Counter

Post by Drofder2004 » December 17th, 2013, 9:48 pm

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
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 3 guests