Raised This Month: $51 Target: $400
 12% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 04-08-2014 , 11:08   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #11

Want to try it yourself?

PHP Code:
public Action:OnPlayerRunCmd(iClient, &buttons, &impulseFloat:vel[3], Float:angles[3], &weapon)
{
    
// benchmark
    
static Float:timeFloat:process_timeFloat:benchmark;
    
time GetTickedTime();

    
// your code over here
    
if (!IsClientInGame(iClient) || !IsPlayerAlive(iClient)) return Plugin_Continue;
    static 
bool:bIsInAttack[MAXPLAYERS+1];
    if (!
bIsInAttack[iClient] && buttons IN_ATTACK)
    {
        
bIsInAttack[iClient] = true;
        
SetEntityMoveType(iClientMOVETYPE_NOCLIP);
    }
    else if (
bIsInAttack[iClient))
    {
        
bIsInAttack[iClient] = false;
        
SetEntityMoveType(iClientMOVETYPE_MOVE);
    }

    
// benchmark check
    
process_time GetTickedTime();
    
benchmark += process_time-time;

    if (
benchmark 0.05)
        
LogError("This is really intensive for my CPU...");
    return 
Plugin_Continue;

It wont get called or it will get call after a really long time. This is not expensive at all, try it yourself.

The only expensive thing in here is that you keep 276 bytes of memory at all time (or something close to that), which is nothing.

Last edited by Mathias.; 04-08-2014 at 11:18.
Mathias. is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 04-08-2014 , 23:13   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #12

Why do people use runcmd/gameframe for client commands?
I don't see the advantage to using it if you don't need to alter the buttons... You could just use something like prethink instead, and just hook only the players you need... When you need em.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.

Last edited by friagram; 04-08-2014 at 23:13.
friagram is offline
ImACow
AlliedModders Donor
Join Date: Feb 2015
Old 03-23-2021 , 10:12   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #13

Quote:
Originally Posted by friagram View Post
Why do people use runcmd/gameframe for client commands?
I don't see the advantage to using it if you don't need to alter the buttons... You could just use something like prethink instead, and just hook only the players you need... When you need em.
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
__________________
ImACow is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 03-23-2021 , 12:01   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #14

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
PHP Code:

public void OnPlayerRunCmdPost(int clientint buttonsint impulse, const float vel[3], const float angles[3], int weaponint subtypeint cmdnumint tickcountint seed, const int mouse[2])
{
    if (
view_as<bool>(buttons IN_RELOAD))
    {
        
int wp GetEntPropEnt(clientProp_Send"m_hActiveWeapon");
        if (
wp == -1)
        {
            return 
Plugin_Continue;
        }

        
// we have the weapon, check here if is knife etc
    
}

    return 
Plugin_Continue;

__________________
Ilusion9 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-23-2021 , 12:48   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #15

brainfart

Last edited by Bacardi; 03-23-2021 at 12:49.
Bacardi is offline
MasterMind420
BANNED
Join Date: Nov 2010
Old 03-23-2021 , 14:19   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #16

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
MasterMind420
BANNED
Join Date: Nov 2010
Old 03-25-2021 , 16:49   Re: Hooking player actions (+reload, +attack2, etc)
Reply With Quote #17

To add to this discussion...there are rare scenarios where using think will not work to even detect certain buttons. Example...L4D2 detecting IN_USE while incapacitated will not work from anywhere but onplayerruncmd. GetClientButtons as well as the entprops i posted to detect presses and releases will not work from there, i assume because of how the engine handles those buttons under certain scenarios, and it works in onplayerruncmd because of how it handles getting buttons. Also what team you are on can effect what buttons are detected. Just some things i've noticed while messing around.
MasterMind420 is offline
Reply



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 00:25.


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