Magazine style reloads: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{lang|Magazine style reloads}}
This will be a short and simple one!
This will be a short and simple one!
Say you want to make each shot and each reload count? In CS you can reload anytime you feel like. Some bullets will be deducted and that is all. However, in real life, you will not start to load the half-empty mag, you pull out a new one, and toss the used one. This system can be found in BF2, and some other games.
Say you want to make each shot and reload count? In CS you can reload anytime you feel like. Some bullets will be deducted and that is all.
 
However, in real life, you will not start to load the half-empty mag, you pull out a new one, and toss the used one. This system can be found in BF2, and some other games.


== Creating a Boolean Variable ==
== Creating a Boolean Variable ==


So now to the code; look up the '''basecombatweapon_shared.h''' and search for "m_bFiresUnderwater" (line 115) to find this code:
So now to the code; look up the '''basecombatweapon_shared.h''' and search for <code>m_bFiresUnderwater</code> (line 115) to find this code:
CNetworkVar( int, m_iClip2 ); // number of shots left in the secondary weapon clip, -1 it not used
<source lang=cpp>
bool m_bFiresUnderwater; // true if this weapon can fire underwater
CNetworkVar( int, m_iClip2 ); // number of shots left in the secondary weapon clip, -1 it not used
bool m_bFiresUnderwater; // true if this weapon can fire underwater
</source>


In-between these two lines you create a variable:
In-between these two lines you create a variable:
CNetworkVar( int, m_iClip2 ); // number of shots left in the secondary weapon clip, -1 it not used
<source lang=cpp highlight=2>
'''bool m_bMagazineStyleReloads; // true if this weapon reloads by removing magazines (remaining bullets)'''
CNetworkVar( int, m_iClip2 ); // number of shots left in the secondary weapon clip, -1 it not used
bool m_bFiresUnderwater; // true if this weapon can fire underwater
bool m_bMagazineStyleReloads; // true if this weapon reloads by removing magazines (remaining bullets)
bool m_bFiresUnderwater; // true if this weapon can fire underwater
</source>




Now go to '''basecombatweapon_shared.cpp''' and search to "m_bFiresUnderwater" (lines 2253 and 2300):
Now go to '''basecombatweapon_shared.cpp''' and search to <code>m_bFiresUnderwater</code> (lines 2253 and 2300):
DEFINE_FIELD( m_bFiresUnderwater, FIELD_BOOLEAN ),
<source lang=cpp>
DEFINE_FIELD( m_bFiresUnderwater, FIELD_BOOLEAN ),
</source>


Once again you need to define it like so:
Once again you need to define it like so:
'''DEFINE_FIELD( m_bMagazineStyleReloads, FIELD_BOOLEAN ),'''
<source lang=cpp highlight=1>
DEFINE_FIELD( m_bFiresUnderwater, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bMagazineStyleReloads, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bFiresUnderwater, FIELD_BOOLEAN ),
</source>




Now that you have the variable set up, look into the CBaseCombatWeapon constructor (line 54) where:
Now that you have the variable set up, look into the <code>CBaseCombatWeapon</code> constructor (line 54) where:
m_bReloadsSingly = false;
<source lang=cpp>
m_bReloadsSingly = false;
</source>


And depending on what you want, you either default magazine style reloads to be on or off:
And depending on what you want, you either default magazine style reloads to be on or off:
m_bReloadsSingly = false;
<source lang=cpp highlight=2>
'''m_bMagazineStyleReloads = false;'''
m_bReloadsSingly = false;
m_bMagazineStyleReloads = false;
</source>


== The Actual Code ==
== The Actual Code ==


Now here's the main part of the code. Look at the ''FinishReload'' procedure (line 1986) and you will find:
Now here's the main part of the code. Look at the <code>FinishReload</code> procedure (line 1986) and you will find:
if ( UsesClipsForAmmo1() )
<source lang=cpp>
{
if ( UsesClipsForAmmo1() )
int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount(m_iPrimaryAmmoType));
{
m_iClip1 += primary;
int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount(m_iPrimaryAmmoType));
pOwner->RemoveAmmo( primary, m_iPrimaryAmmoType);
m_iClip1 += primary;
}
pOwner->RemoveAmmo( primary, m_iPrimaryAmmoType);
}
</source>


Change the last two lines to look like this:
Change the last two lines to look like this:
if ( UsesClipsForAmmo1() )
<source lang=cpp highlight=5>
{
if ( UsesClipsForAmmo1() )
int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount(m_iPrimaryAmmoType));
{
m_iClip1 += primary;
int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount( m_iPrimaryAmmoType ) );
        '''pOwner->RemoveAmmo( m_bMagazineStyleReloads ? min(pOwner->GetAmmoCount(m_iPrimaryAmmoType,GetMaxClip1()) : primary, m_iPrimaryAmmoType);'''
m_iClip1 += primary;
}
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? min( pOwner->GetAmmoCount( m_iPrimaryAmmoType,GetMaxClip1() ) : primary, m_iPrimaryAmmoType );
}
</source>


All that has happened is that if the flag is turned on: you make the clip full again, and remove a whole clip out from the total ammo count.  
All that has happened is that if the flag is turned on: you make the clip full again, and remove a whole clip out from the total ammo count.  
To display the amount of magazines you just have to divide the total amount of ammo by a full clip size and round up.
To display the number of magazines you just have to divide the total amount of ammo by a full clip size and round up.


''Note: this technique "hacks" a magazine style reload instead of creating a native implementation, thus the player can still receive individual bullets.''
{{Note|This technique "hacks" a magazine-style reload instead of creating a native implementation, thus the player can still receive individual bullets.}}


== Covering Your Ass ==
== Covering Your Ass ==


Ever noticed that the game auto-reloads your gun if you holster it? This is not good, especially the way this code is implemented; to remove this go to '''weapon_hl2mpbasehlmpcombatweapon.cpp''' or '''basehlcombatweapon_shared.cpp''' and find the ''ItemHolsterFrame'' procedure. Comment out the
Ever noticed that the game auto-reloads your gun if you holster it? This is not good, especially the way this code is implemented; to remove this go to '''weapon_hl2mpbasehlmpcombatweapon.cpp''' or '''basehlcombatweapon_shared.cpp''' and find the <code>ItemHolsterFrame</code> procedure. Comment out <code>FinishReload();</code>
FinishReload();


== An Example ==
== An Example ==


Now an example to turn on magazine style reloads for any weapon:
Now an example to turn on magazine-style reloads for any weapon:
CWeaponPistol::CWeaponPistol( void )
<source lang=cpp highlight=11>
{
CWeaponPistol::CWeaponPistol( void )
m_flSoonestPrimaryAttack = gpGlobals->curtime;
{
m_flAccuracyPenalty = 0.0f;
m_flSoonestPrimaryAttack = gpGlobals->curtime;
m_flAccuracyPenalty = 0.0f;
m_fMinRange1 = 24;
 
m_fMaxRange1 = 1500;
m_fMinRange1 = 24;
m_fMinRange2 = 24;
m_fMaxRange1 = 1500;
m_fMaxRange2 = 200;
m_fMinRange2 = 24;
m_fMaxRange2 = 200;
'''m_bMagazineStyleReloads = true; // Magazine style reloads'''
 
m_bFiresUnderwater = true;
m_bMagazineStyleReloads = true; // Magazine style reloads
}
m_bFiresUnderwater = true;
}
</source>


== Extension ==
== Extension ==


If you want to only lose 1 of your total ammo instead of the real amount, change:
If you want to only lose 1 of your total ammo instead of the real amount, change:
 
<source lang=cpp>
m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? GetMaxClip1() : primary, m_iPrimaryAmmoType);
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? GetMaxClip1() : primary, m_iPrimaryAmmoType);
 
</source>
to:
to:
 
<source lang=cpp highlight=2>
m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? 1 : primary, m_iPrimaryAmmoType);
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? 1 : primary, m_iPrimaryAmmoType );
</source>


== The Actual Code (FIX) ==
== The Actual Code (FIX) ==
Line 93: Line 114:
And then you reload it becomes (12:0)
And then you reload it becomes (12:0)
It gives you a full clip back instead of the partially full clip you found.
It gives you a full clip back instead of the partially full clip you found.
 
<source lang=cpp>
if ( UsesClipsForAmmo1() )
if ( UsesClipsForAmmo1() )
{
{
int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount(m_iPrimaryAmmoType));
int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount( m_iPrimaryAmmoType ) );
if( pOwner->GetAmmoCount(m_iPrimaryAmmoType) >= GetMaxClip1() ){
if ( pOwner->GetAmmoCount( m_iPrimaryAmmoType ) >= GetMaxClip1() )
m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
{
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? GetMaxClip1() : primary, m_iPrimaryAmmoType);
m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
}else{
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? GetMaxClip1() : primary, m_iPrimaryAmmoType );
m_iClip1 = pOwner->GetAmmoCount(m_iPrimaryAmmoType);
}
pOwner->RemoveAmmo( GetMaxClip1(), m_iPrimaryAmmoType);
else
}
{
}
m_iClip1 = pOwner->GetAmmoCount( m_iPrimaryAmmoType );
pOwner->RemoveAmmo( GetMaxClip1(), m_iPrimaryAmmoType );
}
}
</source>


[[Category:Weapons programming]]
[[Category:Weapons programming]]
[[Category:Programming]]
[[Category:Tutorials]]
[[Category:Tutorials]]

Latest revision as of 11:20, 24 May 2022

English (en)中文 (zh)Translate (Translate)

This will be a short and simple one! Say you want to make each shot and reload count? In CS you can reload anytime you feel like. Some bullets will be deducted and that is all.

However, in real life, you will not start to load the half-empty mag, you pull out a new one, and toss the used one. This system can be found in BF2, and some other games.

Creating a Boolean Variable

So now to the code; look up the basecombatweapon_shared.h and search for m_bFiresUnderwater (line 115) to find this code:

CNetworkVar( int, m_iClip2 );	// number of shots left in the secondary weapon clip, -1 it not used
bool	m_bFiresUnderwater;	// true if this weapon can fire underwater

In-between these two lines you create a variable:

CNetworkVar( int, m_iClip2 );	// number of shots left in the secondary weapon clip, -1 it not used
bool	m_bMagazineStyleReloads;	// true if this weapon reloads by removing magazines (remaining bullets)
bool	m_bFiresUnderwater;	// true if this weapon can fire underwater


Now go to basecombatweapon_shared.cpp and search to m_bFiresUnderwater (lines 2253 and 2300):

DEFINE_FIELD( m_bFiresUnderwater, FIELD_BOOLEAN ),

Once again you need to define it like so:

DEFINE_FIELD( m_bMagazineStyleReloads, FIELD_BOOLEAN ),
DEFINE_FIELD( m_bFiresUnderwater, FIELD_BOOLEAN ),


Now that you have the variable set up, look into the CBaseCombatWeapon constructor (line 54) where:

m_bReloadsSingly	= false;

And depending on what you want, you either default magazine style reloads to be on or off:

m_bReloadsSingly	= false;
m_bMagazineStyleReloads = false;

The Actual Code

Now here's the main part of the code. Look at the FinishReload procedure (line 1986) and you will find:

if ( UsesClipsForAmmo1() )
{
	int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount(m_iPrimaryAmmoType));	
	m_iClip1 += primary;
	pOwner->RemoveAmmo( primary, m_iPrimaryAmmoType);
}

Change the last two lines to look like this:

if ( UsesClipsForAmmo1() )
{
	int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount( m_iPrimaryAmmoType ) );	
	m_iClip1 += primary;
	pOwner->RemoveAmmo( m_bMagazineStyleReloads ? min( pOwner->GetAmmoCount( m_iPrimaryAmmoType,GetMaxClip1() ) : primary, m_iPrimaryAmmoType );
}

All that has happened is that if the flag is turned on: you make the clip full again, and remove a whole clip out from the total ammo count. To display the number of magazines you just have to divide the total amount of ammo by a full clip size and round up.

Note.pngNote:This technique "hacks" a magazine-style reload instead of creating a native implementation, thus the player can still receive individual bullets.

Covering Your Ass

Ever noticed that the game auto-reloads your gun if you holster it? This is not good, especially the way this code is implemented; to remove this go to weapon_hl2mpbasehlmpcombatweapon.cpp or basehlcombatweapon_shared.cpp and find the ItemHolsterFrame procedure. Comment out FinishReload();

An Example

Now an example to turn on magazine-style reloads for any weapon:

CWeaponPistol::CWeaponPistol( void )
{
	m_flSoonestPrimaryAttack = gpGlobals->curtime;
	m_flAccuracyPenalty = 0.0f;

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

	m_bMagazineStyleReloads = true; // Magazine style reloads
	m_bFiresUnderwater	= true;
}

Extension

If you want to only lose 1 of your total ammo instead of the real amount, change:

m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? GetMaxClip1() : primary, m_iPrimaryAmmoType);

to:

m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
pOwner->RemoveAmmo( m_bMagazineStyleReloads ? 1 : primary, m_iPrimaryAmmoType );

The Actual Code (FIX)

If you use the code above then it will work perfectly except... If you have a full clip that looks like this (12:6) And then you shoot to (3:6) And then you reload it becomes (12:0) It gives you a full clip back instead of the partially full clip you found.

if ( UsesClipsForAmmo1() )
{
	int primary = min( GetMaxClip1() - m_iClip1, pOwner->GetAmmoCount( m_iPrimaryAmmoType ) );	
	if ( pOwner->GetAmmoCount( m_iPrimaryAmmoType ) >= GetMaxClip1() )
	{
		m_iClip1 = m_bMagazineStyleReloads ? GetMaxClip1() : m_iClip1 + primary;
		pOwner->RemoveAmmo( m_bMagazineStyleReloads ? GetMaxClip1() : primary, m_iPrimaryAmmoType );
	}
	else
	{
		m_iClip1 = pOwner->GetAmmoCount( m_iPrimaryAmmoType );
		pOwner->RemoveAmmo( GetMaxClip1(), m_iPrimaryAmmoType );
	}
}