Here's a quick and easy tutorial on how to implement a rushed gun reloading mechanism.
In a nutshell, when you reload, you lose your mag and any bullets left inside.
Nice feature if you want to make a realistic mode (have you ever removed bullets from large-size mags and put them back in another one after?? If so, you know it doesn't take 1 second to do).
I'm using the latest version of SamVanheer SDK (half-life updated)
First, open weapons_shared.cpp and go to Line 120.
You will see the following:
void CBasePlayerWeapon::ItemPostFrame()
{
if ((m_fInReload) && (m_pPlayer->m_flNextAttack <= UTIL_WeaponTimeBase()))
{
// complete the reload.
int j = V_min(iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]);
// Add them to the clip
m_iClip += j;
m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= j;
To better understand how this code works.
int j
= we are looking for how many bullets we need in our current clip to get a full clip (or the maximum ammo amounts available)V_min(a,b)
= "j" will be either the necessary amount to make a full clip OR the remaining ammo amount left. (You can't get a full Glock clip if you only have 3 bullets in your inventory)- Now that we know "j" we add the amount to the current clip (
m_iClip += j;
) - Obviously, we subtract the amount from our ammo inventory ( `m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= j;` )
Before starting, let's see what we need:
- If I have 12/17 bullets left in my Glock and I reload, I want to get 17 (max amount) if possible
- If not possible, I want to get my only non-full mag. (If currently at 12/17 in my mag and I have 13 in my inventory, I'll get a mag with 13/17 and be at 0 in my inventory).
- Any mag dropped is lost permanently. (If currently at 12/17 and I have 117 in my inventory, I'll be at 17/17 and 100, so I lost 12 bullets in my dropped mag).
if
and else
statements.Here's how it looks. (Replace the previous lines with these)
void CBasePlayerWeapon::ItemPostFrame()
{
if ((m_fInReload) && (m_pPlayer->m_flNextAttack <= UTIL_WeaponTimeBase()))
{
// get j count
int j = V_min(iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]);
// Add a full clip and permanently remove the previous one if a full clip is available
// m_iClip = MaxClip(); could work as well.
if (m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] >= iMaxClip())
{
m_iClip += j;
m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= iMaxClip();
}
// Add the only remaining clip (full or not) and permanently remove the previous one.
else
{
m_iClip = m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType];
m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] = 0;
}
Now, the only thing to do is to ensure that none of your guns share the same type of ammo.For Original Half-Life, you must create a specific 9mm ammo type for either the Glock (9mm_glock) or the mp5 (9mm_mp5).
With this feature, if you drop a mp5 mag, it shouldn't affect your 9mm mags.
To create Custom Ammo Types you can refer to *this tutorial*
I hope it helps!