Raised This Month: $ Target: $400
 0% 

Hooking player actions (+reload, +attack2, etc)


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
MasterMind420
BANNED
Join Date: Nov 2010
Old 03-23-2021 , 14:19   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #15

Quote:
Originally Posted by ImACow View Post
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));
}

Last edited by MasterMind420; 03-23-2021 at 14:57.
MasterMind420 is offline
 



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 13:12.


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