Quote:
Originally Posted by ImACow
How would one hook into client commands, without the runcmd/gameframe?
I'm interested in detecting if a player presses "+reload" on their knife / gun
|
This should do what you want...adjust the checks for what u need. Like friagram said unless you need to change the buttons use think instead.
Code:
int iActiveWeapon[MAXPLAYERS+1];
char sActiveWpnClsName[MAXPLAYERS+1][32];
public void OnClientPutInServer(int client)
{
//Put checks in here for the players you want hooked
SDKHook(client, SDKHook_PreThink, OnPreThink);
SDKHook(client, SDKHook_WeaponSwitchPost, OnWeaponSwitchPost);
}
public void OnPreThink(int client)
{
if (IsClientInGame(client) && IsPlayerAlive(client)) //Adjust checks for what you need
{
//Presses or holds reload button
int buttons = GetClientButtons(client);
if (buttons & IN_RELOAD)
//Or pressed reload button
if (GetEntProp(client, Prop_Data, "m_afButtonPressed") & IN_RELOAD)
//Or released reload button
if (GetEntProp(client, Prop_Data, "m_afButtonReleased") & IN_RELOAD)
{
int iWeapon = iActiveWeapon[client];
if (IsValidWeapon(iWeapon))
{
if (StrContains(sActiveWpnClsName[client], "knife") > -1)
{
//Do whatever you want here
}
}
}
}
}
//This gets the active weapons index and classname globally when switched to
//so you are not constantly getting it within think and they can be retrieved anywhere in your plugin.
public Action OnWeaponSwitchPost(int client, int weapon)
{
if (IsClientInGame(client) && IsPlayerAlive(client)) //Adjust checks for what you need
{
iActiveWeapon[client] = 0;
sActiveWpnClsName[client][0] = '\0';
int iWeapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
if (IsValidWeapon(iWeapon))
{
GetEntityClassname(iWeapon, sActiveWpnClsName[client], sizeof(sActiveWpnClsName));
iActiveWeapon[client] = iWeapon;
}
}
}
stock bool IsValidWeapon(int weapon)
{
return (weapon > 0 && IsValidEntity(weapon));
}