View Single Post
Nite
Junior Member
Join Date: Dec 2007
Old 06-25-2009 , 03:39   Re: g_pGameRules question
Reply With Quote #2

This is how I hook gamerules (in TF2). I basically hook by virtual table index. One of functions I hook in TF2 from CTFGameRules class is FlPlayerFallDamage (returns a float or amount of fall damage taken).

Code:
58    CTFGameRules::FlPlayerFallDamage(CBasePlayer *)
So I first declare it like other hooks:

Code:
SH_DECL_MANUALHOOK1(GR_FlPlayerFallDamage, 58, 0, 0, float, CBasePlayer*);
Then I hook it in other function that is part of a class (CGamerulesHooks) to deal with hooking gamerules functions.

Code:
//to hook
void CGamerulesHooks::SetupHooks()
{
   SH_ADD_MANUALHOOK_MEMFUNC(GR_FlPlayerFallDamage, g_pGameRules, this, &CGamerulesHooks::Hook_FlPlayerFallDamage, false);
}
 
//to unhook
void CGamerulesHooks::RemoveHooks()
{
   SH_REMOVE_MANUALHOOK_MEMFUNC(GR_FlPlayerFallDamage, g_pGameRules, this, &CGamerulesHooks::Hook_FlPlayerFallDamage, false);
}
I unhook gamerules functions at LevelShutdown. Then on ServerActivate I re-obtain gamerules pointer and rehook the gamerules functions (not sure if that is needed but I've been doing this for a while to be on safe side). One thing I noticed is I have to delay the signature scan for it for about 0.10 seconds after ServerActivate, otherwise the pointer is NULL for some reason.

Hope that helps.
Nite is offline