View Single Post
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