Raised This Month: $32 Target: $400
 8% 

[TF2] Hooking when a player fires a weapon


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ShadowMarioBR
Member
Join Date: Feb 2018
Old 08-27-2019 , 21:40   [TF2] Hooking when a player fires a weapon
Reply With Quote #1

So, tWeaponShootPosition didn't worked in my Windows machine, so... using VTable Dumper i've tried DHooking CBasePlayer::OnMyWeaponFired myself for the first time, and it worked perfectly as a replacement for it.

What's it's functionality?
OnMyWeaponFired is a function called when you... shot your weapon/fire your melee, as the name says...
It can also be used as a replacement for CalcAttackIsCritical if you just want to know when a player shoot
Might make a plugin with a forward for it someday, but for now...
Here's the example code:
Code:
#pragma semicolon 1
#include <sourcemod>
#include <dhooks>
#pragma newdecls required

Handle Hook_OnMyWeaponFired;

public void OnPluginStart()
{
	Handle config = LoadGameConfigFile("tf2.onmyweaponfired");
	
	int offset = GameConfGetOffset(config, "CBasePlayer::OnMyWeaponFired");
	if (offset == -1)
		SetFailState("Missing offset for CBasePlayer::OnMyWeaponFired");
	
	Hook_OnMyWeaponFired = DHookCreate(offset, HookType_Entity, ReturnType_Void, ThisPointer_CBaseEntity, OnMyWeaponFired);
	DHookAddParam(Hook_OnMyWeaponFired, HookParamType_Int);
	
	delete config;
	
	for (int i = 1 ; i <= MaxClients ; i++)
		if(IsClientInGame(i))
			OnClientPutInServer(i);
}

public void OnClientPutInServer(int client)
{
	DHookEntity(Hook_OnMyWeaponFired, true, client);
}

public MRESReturn OnMyWeaponFired(int client, Handle hReturn, Handle hParams)
{
	if(client < 1 || client > MaxClients || !IsValidEntity(client) || !IsPlayerAlive(client))
		return MRES_Ignored;
	
	// Your code here
	// You can also use GetEntProp(client, Prop_Send, "m_hActiveWeapon") for getting the weapon the player shoot with
	return MRES_Ignored;
}
NOTE: I were in a Windows machine and don't have a Linux one, so i didn't tested the Linux offset, but it should be working
NOTE 2: You're going to need DHooks for getting this working

Gamedata file download:
Attached Files
File Type: txt tf2.onmyweaponfired.txt (139 Bytes, 138 views)

Last edited by ShadowMarioBR; 08-28-2019 at 15:28. Reason: Fixed typo
ShadowMarioBR is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 08-27-2019 , 22:40   Re: [TF2] Hooking when a player fire a weapon
Reply With Quote #2

Do you mind if I build this into my API plugin? (found here: https://github.com/Drixevel/TF2-API)
Drixevel is offline
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 08-28-2019 , 10:25   Re: [TF2] Hooking when a player fire a weapon
Reply With Quote #3

Neat work man wish I had this when I was into TF2.
Halt is offline
ShadowMarioBR
Member
Join Date: Feb 2018
Old 08-28-2019 , 12:58   Re: [TF2] Hooking when a player fire a weapon
Reply With Quote #4

Quote:
Originally Posted by Drixevel View Post
Do you mind if I build this into my API plugin? (found here: https://github.com/Drixevel/TF2-API)
Definitely don't mind, you're free for using it!

Last edited by ShadowMarioBR; 08-28-2019 at 15:21.
ShadowMarioBR is offline
Reply


Thread Tools
Display Modes

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 22:29.


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