Hooks
Hooks are a form of events that scripts can hook into. When you register a hook, the function you’ve registered will be called whenever the associated event occurs.
Multiple scripts can hook into the same hook, and the same script can hook the same hook multiple times. Map scripts will always come first when hooks that both map scripts and plugins can use are executed.
1 Hook return code
All hooks have to return a code to indicate whether they’ve handled the event or not.
If a hook has handled the event, it should return HOOK_HANDLED
. Otherwise, it should return HOOK_CONTINUE
.
2 Hook definition and usage
All hooks are defined in the namespace “Hooks”.
All hooks have funcdefs defined for them that have the syntax <name>Hook, where <name> is the name of the constant without any namespaces prepended. For example, the Hooks::Game::MapChange
hook has the funcdef MapChangeHook
. This can be used to register object methods as hooks.
You can get an up to date list of hook by typing as_dumphooks <filename> in the console. You must be the server admin to be able to do this. The filename should not have an extension. The file extension is .txt.
2.1 Registering hooks
To register a hook, you must use the g_Hooks global object. This object is defined as follows:
CModuleHookManager g_Hooks;
(The actual definition uses a global property accessor, but it behaves like a variable as shown above)
Registering a hook is done as follows:
HookReturnCode MapChange( const string& in szNextMap ) { // Handle map change } g_Hooks.RegisterHook( Hooks::Game::MapChange, @MapChange );
Removing a hook is done much like registering it:
g_Hooks.RemoveHook( Hooks::Game::MapChange, @MapChange );
It is also possible to remove all of the hook functions registered for a particular hook in the current script by calling RemoveHook with just the hook ID parameter:
g_Hooks.RemoveHook( Hooks::Game::MapChange );
If you are trying to register a method to a hook, you need to pass the method handle in via a delegate, this is done as follows:
g_Hooks.RegisterHook( Hooks::Game::MapChange, MapChangeHook( obj.MapChange ) )
2.2 Stop Modes
Hooks can have one of a few stop modes. This mode defines when hook execution should stop.
Name | Action |
---|---|
ON_HANDLED
|
As soon as a hook has returned HOOK_HANDLED, stop execution. |
MODULE_HANDLED
|
As soon as a single script has return HOOK_HANDLED from any of its hooks, stop execution. |
CALL_ALL
|
Regardless of the return code, execute all hooks. |
3 Available hooks
3.1 Game Hooks
Hooks::Game::
Constant | Signature | Description | Stop Mode | Availability |
---|---|---|---|---|
MapChange
|
HookReturnCode Function( const string& in szNextMap )
|
When the map changes, this hook is called. Note that this happens when the world is destroyed. There may still be entities that exist at this point. This is caused by engine limitations. | CALL_ALL
|
All |
EntityCreated
|
HookReturnCode Function( CBaseEntity@ pEntity )
|
Called when a new entity is created. At this point the entity is not spawned yet and may not be fully initialized. | ON_HANDLED
|
All |
3.2 Player Hooks
Hooks::Player::
Constant | Signature | Description | Stop Mode | Availability |
---|---|---|---|---|
CanPlayerUseReservedSlot | HookReturnCode Function( edict_t@ pEntity, const string& in szPlayerName, const string& in szIPAddress, bool& out bAllowJoin ) | Called when a player connects to the server, and the number of slots left on the server is <= the number of reserved slots. Set bAllowJoin to true to allow the player to join (default false). Note: At this point in time the player’s CBasePlayer instance does not yet exist. Do not try to use it, as it will not work. |
ON_HANDLED | Plugins only |
ClientConnected | HookReturnCode Function( edict_t@ pEntity, const string& in szPlayerName, const string& in szIPAddress, bool& out bDisallowJoin, string& out szRejectReason ) | Called when a player connects to the server. if bDisallowJoin is set to false, the player is disconnected. szRejectReason is shown to the player if disconnected. the maximum length of the reject reason string is 127 characters. Note: At this point in time the player’s CBasePlayer instance does not yet exist. Do not try to use it, as it will not work. |
CALL_ALL | Plugins only |
ClientPutInServer | HookReturnCode Function( CBasePlayer@ pPlayer ) | Called when a player has finished connecting and is put into the world. It is safe to send network messages to the player at this point. The player’s CBasePlayer instance is created right before this hook is executed. |
CALL_ALL | All |
ClientDisconnect | HookReturnCode Function( CBasePlayer@ pPlayer ) | Called when a player disconnects. Note that this is only called if the player was fully connected, meaning the player went through ClientPutInServer. This is never called for the local host, since it can’t disconnect before shutting the server itself down. | CALL_ALL | All |
ClientSay | HookReturnCode Function( SayParameters@ pParams ) | Called when a player says something in game chat. The SayParameters class can be used to manipulate input and veto the message. | ON_HANDLED | All |
GetPlayerSpawnSpot | HookReturnCode Function( CBasePlayer@ pPlayer, CBaseEntity@& out ppEntSpawnSpot ) | Called when the game is trying to find a spawn spot for a player. To specify a spawn spot (or no spawn spot), set ppEntSpawnSpot to the spot you wish to use, and return HOOK_HANDLED. Setting null will cause the game to spawn the player dead. | ||
PlayerSpawn | HookReturnCode Function( CBasePlayer@ pPlayer ) | Called when a player (re)spawns. Player render settings and equipment changes are unavailable at this point. | CALL_ALL | All |
PlayerCanRespawn | HookReturnCode Function( CBasePlayer@ pPlayer, bool& out bCanRespawn ) | Called when the game wants to know if the player should be able to respawn or not. Set bCanRespawn to false to disallow, default true. | ON_HANDLED | All |
PlayerTakeDamage | HookReturnCode PlayerTakeDamageHook( DamageInfo@ pDamageInfo ) | Called when a player takes damage. Note that the victim entity can’t be changed at this point. | All | |
PlayerKilled | HookReturnCode Function( CBasePlayer@ pPlayer, CBaseEntity@ pAttacker, int iGib ) | Called when a player is killed. pAttacker is the entity responsible for killing the player, and may be null. iGib is one of the GIB enum values, indicating whether or not the player should be gibbed or not. | All | |
PlayerRevived
|
HookReturnCode Function( CBasePlayer@ pPlayer )
|
Called when a player is revived. At this point all the revival process is finished. | CALL_ALL | All |
PlayerUse | HookReturnCode Function( CBasePlayer@ pPlayer, uint& out uiFlags ) | Called when the game is processing player button input. Note that this occurs even if the player has not pressed any keys. uiFlags is used to skip player use processing after the hook returns. Setting the flag PlrHook_SkipUse will skip processing. | All | |
PlayerPreThink | HookReturnCode Function( CBasePlayer@ pPlayer, uint& out uiFlags ) | Called when the player is processing pre think events. uiFlags is used to skip pre think processing after the hook returns. Setting the flag PlrHook_SkipVehicles will skip vehicle processing. | ||
PlayerPostThink | HookReturnCode Function( CBasePlayer@ pPlayer ) | Called when the player is processing post think events. | ||
PlayerPreDecal | HookReturnCode Function( CBasePlayer@, const TraceResult& in, bool& out bResult) | Called when a player attempts to spraypaint a decal onto a surface. The given trace result contains the surface information. Set bResult to false if the player shouldn’t be able to spray. | ||
PlayerDecal | HookReturnCode Function( CBasePlayer@ pPlayer, const TraceResult& in trace ) | Called when a player is spraypainting a decal onto a surface. The given trace result contains the surface information. | ||
PlayerEnteredObserver | HookReturnCode Function( CBasePlayer@ pPlayer ) | Called when a player enters observer mode. | ||
PlayerLeftObserver | HookReturnCode Function( CBasePlayer@ pPlayer ) | Called when a player leaves observer mode. |
3.3 Monster Hooks
Hooks::Monster::
Constant | Signature | Description | Stop Mode | Availability |
---|---|---|---|---|
MonsterKilledHook | HookReturnCode Function( CBaseMonster@ pMonster, CBaseEntity@ pAttacker, int iGib ) | Called when a monster is killed (This hook isn’t called for monster_sentry/monster_barnacle). This hook may have spam issues. |
ON_HANDLED | All |
MonsterTakeDamage | HookReturnCode Function( DamageInfo@ pDamageInfo ) | Called when a monster takes damage. | ON_HANDLED | All |
3.4 Weapon Hooks
Hooks::Weapon::
Constant | Signature | Description | Stop Mode | Availability |
---|---|---|---|---|
WeaponPrimaryAttack | HookReturnCode Function( CBasePlayer@ pPlayer, CBasePlayerWeapon@ pWeapon ) | Called when a player fires a weapon's primary attack. | ON_HANDLED | All |
WeaponSecondaryAttack | HookReturnCode Function( CBasePlayer@ pPlayer, CBasePlayerWeapon@ pWeapon ) | Called when a player fires a weapon's secondary attack. | ON_HANDLED | All |
WeaponTertiaryAttack | HookReturnCode Function( CBasePlayer@ pPlayer, CBasePlayerWeapon@ pWeapon ) | Called when a player fires a weapon's tertiary attack. | ON_HANDLED | All |
WeaponReloadHook | HookReturnCode Function( CBasePlayer@ pPlayer, CBasePlayerWeapon@ pWeapon ) | Called a player reloads a weapon (or calls BaseClass::Reload()). | CALL_ALL | All |
3.5 Pickup Hooks
Hooks::PickupObject::
Constant | Signature | Description | Stop Mode | Availability |
---|---|---|---|---|
Materialize | HookReturnCode Function( CBaseEntity@ pPickup ) | Called when a pickup object materializes. | CALL_ALL | All |
CanCollect | HookReturnCode Function( CBaseEntity@ pPickup, CBaseEntity@ pOther, bool& out bResult ) | Called when a pickup object is about to be collected by a player. Note that basic checks are done before this hook is called. Set bResult to false if the player shouldn’t be able to pick up the object. | ON_HANDLED | All |
Collected | HookReturnCode Function( CBaseEntity@ pPickup, CBaseEntity@ pOther ) | Called when a pickup object is collected by a player. | CALL_ALL | All |