Raised This Month: $32 Target: $400
 8% 

[TUT] How to hook reload event properly


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-07-2010 , 13:49   [TUT] How to hook reload event properly
Reply With Quote #1

This simple snippet will show you how to hook reload event including shotguns properly. If you have any questions, feel free to ask.
Code:
#include < amxmodx > #include < fakemeta > #include < hamsandwich > const m_pPlayer          = 41; const m_fInReload        = 54; const m_fInSpecialReload = 55; const m_flTimeWeaponIdle = 48; public plugin_init( ) {     // 2 = CSW_SHIELD = UNDEFINED | PUT SHOTGUNS HERE TO SKIP IN LOOP AND REGISTER MANUALLY     new const NO_RELOAD = ( 1 << 2 ) | ( 1 << CSW_KNIFE ) | ( 1 << CSW_C4 ) | ( 1 << CSW_M3 ) |         ( 1 << CSW_XM1014 ) | ( 1 << CSW_HEGRENADE ) | ( 1 << CSW_FLASHBANG ) | ( 1 << CSW_SMOKEGRENADE );         new szWeaponName[ 20 ];         for( new i = CSW_P228; i <= CSW_P90; i++ )     {         // If its no-reload weapon or shotgun skip it         if( NO_RELOAD & ( 1 << i ) )         {             continue;         }                 // Get weapon name         get_weaponname( i, szWeaponName, 19 );                 // Register forward         RegisterHam( Ham_Weapon_Reload, szWeaponName, "FwdHamWeaponReload", 1 );     }         // Register shotguns, we need different function for additional checks     RegisterHam( Ham_Weapon_Reload, "weapon_m3",     "FwdHamShotgunReload", 1 );     RegisterHam( Ham_Weapon_Reload, "weapon_xm1014", "FwdHamShotgunReload", 1 ); } public FwdHamWeaponReload( const iWeapon ) {     // m_fInReload is set to TRUE in DefaultReload( )     // We are only checking if this offset is set to true because     // Ham_Weapon_Reload is being called as long as player holds the reload key     // But its only called once if weapon is being reloaded, and doesnt call more while its reloading     if( get_pdata_int( iWeapon, m_fInReload, 4 ) )     {         // Weapon has started reload                 // To check if its reload by player or auto (0 ammo) you can do this:         //  Check !( get_user_button( id ) & IN_BUTTON )         //  Or check if clip ammo equals to 0     } } public FwdHamShotgunReload( const iWeapon ) {     // in Reload( ) there is switch of this offset     // On case 0 it is being set to 1 and some weapon idle/nextattacks are being set to 0.55     // case 1: bullet input animation     // case 2: adding bullet to clip itself     // this case 1-2-1 is being looped while the shotgun is reloading     if( get_pdata_int( iWeapon, m_fInSpecialReload, 4 ) != 1 )     {         return;     }         // If you will remove the check below, you will be able to hook when engine     // sets animation (M3_INSERT) bullet         // The first set of m_fInSpecialReload to 1. m_flTimeWeaponIdle remains 0.55 set from Reload( )     new Float:flTimeWeaponIdle = get_pdata_float( iWeapon, m_flTimeWeaponIdle, 4 );         if( flTimeWeaponIdle != 0.55 )     {         return;     }         // Shotgun has started reload }
__________________

Last edited by xPaw; 12-15-2012 at 06:35.
xPaw is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-07-2010 , 13:50   Re: How to hook reload event properly
Reply With Quote #2

Code involved:
Reload for M3:
Code:
    void CM3::Reload()     {         if ( m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] <= 0 || m_iClip == SHOTGUN_MAX_CLIP )         {             return;         }         if ( m_flNextPrimaryAttack > UTIL_WeaponTimeBase() )         {             return;         }
        switch ( m_fInSpecialReload )
        {             case 0 :             {                 m_pPlayer->SetAnimation( PLAYER_RELOAD );                   SendWeaponAnim( M3_START_RELOAD, UseDecrement() );
                m_fInSpecialReload = 1;
                m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.55;
                m_flTimeWeaponIdle        = UTIL_WeaponTimeBase() + 0.55;
                m_flNextPrimaryAttack     = UTIL_WeaponTimeBase() + 0.55;                 m_flNextSecondaryAttack   = UTIL_WeaponTimeBase() + 0.55;             }             case 1 :             {                    if ( m_flTimeWeaponIdle > gpGlobals->time )                 {                     return;                 }
                m_fInSpecialReload = 2;
                  SendWeaponAnim( M3_INSERT, UseDecrement() );                   m_flNextReload     = UTIL_WeaponTimeBase() + 0.45;                 m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.45;             }             default :             {                 m_iClip += 1;                 m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] -= 1;
                m_fInSpecialReload = 1;
                  m_pPlayer->ammo_buckshot--;             }         }     }
Reload for weapons (I used ak47 as example, they mostly equal to each other):
Code:
void CAK47::Reload() {
    if ( m_pPlayer->ammo_762nato && DefaultReload( AK47_MAX_CLIP, AK47_RELOAD, 2.45 ) )
    {         m_pPlayer->SetAnimation( PLAYER_RELOAD );         m_flAccuracy   = 0.2;         m_iShotsFired  = 0;         m_bDelayFire   = 0;     } }
DefaultReload which is being called from any weapon reload (except shotguns)
Code:
BOOL CBasePlayerWeapon :: DefaultReload( int iClipSize, int iAnim, float fDelay, int body ) {     if (m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] <= 0)         return FALSE;     int j = min(iClipSize - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]);      if (j == 0)         return FALSE;     m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + fDelay;     //!!UNDONE -- reload sound goes here !!!     SendWeaponAnim( iAnim, UseDecrement(), body );
    m_fInReload = TRUE;
    m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 3;     return TRUE; }
__________________

Last edited by xPaw; 12-15-2012 at 06:36.
xPaw is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-07-2010 , 14:05   Re: How to hook reload event properly
Reply With Quote #3

Nice to see CSSDK is useful.
__________________
Arkshine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-07-2010 , 14:09   Re: How to hook reload event properly
Reply With Quote #4

Reload code was the same in HLSDK which i used it weapon max clip plugin.
Example on how to hook reload (Item_Preframe) if someone is interested : http://forums.alliedmods.net/showthread.php?p=728613
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-07-2010 , 14:18   Re: How to hook reload event properly
Reply With Quote #5

Quote:
Originally Posted by ConnorMcLeod View Post
Reload code was the same in HLSDK which i used it weapon max clip plugin.
Hm true, haven't looked closely into those functions

Quote:
Originally Posted by Arkshine View Post
Nice to see CSSDK is useful.
Yes its really helpful ;)
__________________
xPaw is offline
Immortal_BLG
Member
Join Date: Feb 2010
Location: RUSSIA
Old 04-10-2010 , 05:25   Re: How to hook reload event properly
Reply With Quote #6

xPaw you forgot to subtract the current time from the m_flTimeWeaponIdle, so check
Code:
if( flTimeWeaponIdle != 0.55 )
is wrong!!!
Immortal_BLG is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-10-2010 , 06:10   Re: How to hook reload event properly
Reply With Quote #7

Quote:
Originally Posted by Immortal_BLG View Post
xPaw you forgot to subtract the current time from the m_flTimeWeaponIdle, so check
Code:
if( flTimeWeaponIdle != 0.55 )
is wrong!!!
Why?
__________________
xPaw is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-10-2010 , 06:22   Re: How to hook reload event properly
Reply With Quote #8

He's not aware that UTIL_WeaponTimeBase() is set to 0 for client weapons, that's why you get the value alone and not with gametime included.

btw :

Code:
float UTIL_WeaponTimeBase( void ) { #if defined( CLIENT_WEAPONS )     return 0.0; #else     return gpGlobals->time; #endif }
__________________
Arkshine is offline
Immortal_BLG
Member
Join Date: Feb 2010
Location: RUSSIA
Old 04-10-2010 , 07:59   Re: How to hook reload event properly
Reply With Quote #9

"He's ...." - It's me? If so, then perhaps we are not talking about server side?
Immortal_BLG is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-10-2010 , 08:57   Re: How to hook reload event properly
Reply With Quote #10

Not sure to understand what you say. What I was meant is CS is compiled to use the predicted weapons, that's why CLIENT_WEAPONS is defined and that's why UTIL_WeaponTimeBase() returns 0.0. m_flTimeWeaponIdle will return directly 0.55. XPawn's code is fine.
__________________
Arkshine is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode