Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/sites/a/ on line 20
Wavelength | Your Game Editing Resource | Three Firing Modes for Pistol
Welcome, Guest! Login | Register

Three Firing Modes for Pistol [Print this Article]
Posted by: minmaijd
Date posted: Mar 18 2005
User Rating: 4 out of 5.0
Number of views: 8722
Number of comments: 2
Description: Explains how to create 3 firing modes for the pistol: semi-automatic, burst-fire, and fully-automatic.
The purpose of this tutorial is to explain how to create 3 firing modes for the pistol: semi-automatic,

burst-fire, and fully-automatic. The way it works in gameplay is, primary attack fires semi, and secondary

can fire either full-auto or burst, based on firing mode.

Also, I started this tutorial before I found out about 'CHLSelectFireMachineGun'... either way, it functions far different and allows for as many

firing modes as you would like with my method.

So, to begin, a control needs to be set-up for changing firing modes. The following link is a great

tutorial for creating the key. It also goes into setting up the RPG for using the mode, but that half of

his tutorial does not apply to this tutorial naturally as we are dealing with the pistol.

Implementing Rate of Fire or Change Fire Mode

Once you have initialized a key for firing mode switch, you must implement that key within the weapon file

which will be utilizing it. In our case the file is weapon_pistol.cpp

Step 1 Initialization:

Go to the following piece of code:

 CODE (C++) 
    float   m_flSoonestPrimaryAttack;
    float   m_flLastAttackTime;
    float   m_flAccuracyPenalty;
    int     m_nNumShotsFired;

Below this insert the following:

 CODE (C++) 
    float m_flNextFireModeChange;   // indicates when the player can change the firing mode again
    int   nFireCount;       // determines bullet index within the burst fire (up to 3)
    bool  bFiring;          // used to indicate when firing in full-auto
    bool  bFireMode;        // true means the gun is in full-auto, false = burst-fire

    void    checkCount();       // checks firing mode and bullet index for burst fire
    void    FireMode();     // used to switch firing mode

Next go to the data description initializations:

 CODE (C++) 

    DEFINE_FIELD( m_flSoonestPrimaryAttack, FIELD_TIME ),
    DEFINE_FIELD( m_flLastAttackTime,   FIELD_TIME ),

    DEFINE_FIELD( m_flAccuracyPenalty,  FIELD_FLOAT ), //NOTENOTE: This is NOT tracking game time
    DEFINE_FIELD( m_nNumShotsFired,     FIELD_INTEGER ),

Add the following to the data description:

 CODE (C++) 
    DEFINE_FIELD( m_flNextFireModeChange,   FIELD_TIME ),
    DEFINE_FIELD( nFireCount,       FIELD_INTEGER ),
    DEFINE_FIELD( bFiring,          FIELD_BOOLEAN ),
    DEFINE_FIELD( bFireMode,        FIELD_BOOLEAN ),

It's still unclear to me why exactly this is necessary, but it is necessary.

Last, give values to the variables. After the following:

 CODE (C++) 
CWeaponPistol::CWeaponPistol( void )
    m_flSoonestPrimaryAttack = gpGlobals->curtime;
    m_flAccuracyPenalty = 0.0f;

    m_fMinRange1        = 24;
    m_fMaxRange1        = 1500;
    m_fMinRange2        = 24;
    m_fMaxRange2        = 200;

    m_bFiresUnderwater  = true;

Add this:

 CODE (C++) 
    nFireCount          = 3;        // this keeps gun from firing when first equipped
    bFiring             = false;
    bFireMode           = false;    // initializes to burst-fire
    m_flNextFireModeChange = gpGlobals->curtime; // allow for first mode change

Step 2 Calling checkCount():

Checkcount must be checked every frame so include a call to it from CWeaponPistol::ItemPostFrame

like so:

 CODE (C++) 
void CWeaponPistol::ItemPostFrame( void )

Step 3 Setting up the functions:

Goto the very bottom of the file and inlude the following:

 CODE (C++) 
void CWeaponPistol::FireMode( void )    // switches firing modes
    if ( m_flNextFireModeChange < gpGlobals->curtime ) // check that time has elapsed since last switch
        nFireCount = 3;         // set the bullet index to 3 to prevent firing on switch
        bFireMode = !bFireMode;     // switch firing mode
        m_flNextFireModeChange = gpGlobals->curtime + 0.5f;  // wait time of .5s for next switch

And below this add:

 CODE (C++) 
void CWeaponPistol::checkCount( void )  // checks firing modes and bullet index for burst fire
    CBasePlayer *pPlayer = ToBasePlayer( GetOwner() );

    if (pPlayer == NULL)        // check to see if player is present

    if (pPlayer->m_afButtonPressed & IN_ATTACK2)
        if(bFireMode)           // if full auto is on set bFiring to true
            bFiring = true;
        nFireCount = 0;
    }else if (pPlayer->m_afButtonReleased & IN_ATTACK2)
        bFiring = false;
    // if in burst fire mode and timing correct, then count off 3 frames... firing each frame
    if((bFireMode==false)&&(gpGlobals->curtime - m_flLastAttackTime >= 0.1f)){
        if(nFireCount < 3){
    // if "firing" in Full autO and correct timing then fire every fire (executes every frame)
    if((bFiring)&&(gpGlobals->curtime - m_flLastAttackTime >= 0.1f))

Step 4:
Find the reload function and add 'bFiring = false':
 CODE (C++) 
bool CWeaponPistol::Reload( void )
    bool fRet = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
    if ( fRet )
        bFiring = false;            // adding this ensures that automatic fire is not taking place during reload
        WeaponSound( RELOAD );
        m_flAccuracyPenalty = 0.0f;

    return fRet;

So! That's it! However, to ensure that the fireMode key is to work properly open

"/SourceMods/ModName/cfg/config.cfg" and make sure that "x" or whichever key you want is bound to

"+firemode". Another note... sort of glitch/feature is when you press and hold primary fire, then hold

secondary, when you release secondary several bullets will be released at once. If you have a fix, please


Rate This Article
This article is currently rated: 4 out of 5.0 (1 Votes)

You have to register to rate this article.
User Comments Showing comments 1-2

Posted By: imatard on Mar 26 2005 at 20:13:49
I actually did a similar firing mode to the machine guns. I had the same bug where if you held the primary fire and touched the firing mode button, the guns would unload the entirety of their clips, until i was out of ammo. Give this link a try

Posted By: Drakeling on Apr 11 2005 at 05:44:02
Very Nice...

A few things to note:

- If you follow the tutorial you posted in addition to this article, implementing the check for IN_FIREMODE inside the basecombatweapon_shared is not only unnessessary but it will not compile. Instead of doing the input check on baseweapon's ItemPostFrame, do it in each weapon's individual ItemPostFrame that have FireModes.
- Secondly, if you do follow the mentioned article, then you do not need the function prototype void FireMode();

Also, it took me some time to realize just what your modifcation did... so obviously in game instruction would be needed if this were to be added to a mod of sorts. I plan on adding a small glow effect (can be found in the ar2 combine ball attack code ar2.cpp, where it flashes the screen white) to make the screen go slightly yellow for a split second and play a sound effect when weapon mode is changed.

Thanks for the useful information ^^ I appriciate your time and effortEdited by Drakeling on Apr 11 2005, 05:45:11

You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 05 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005

Latest Comments
knock knock
General | News
By: MIFUNE | Dec 31 2017
knock knock
General | News
By: omega | Dec 22 2016
knock knock
General | News
By: MIFUNE | Oct 10 2015
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
knock knock
General | News
By: Whistler | Nov 05 2011
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (5 guests)
About - Credits - Contact Us

Wavelength version:
Valid XHTML 1.0! Valid CSS!