AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [TF2] Hooking when a player fires a weapon (https://forums.alliedmods.net/showthread.php?t=318376)

ShadowMarioBR 08-27-2019 21:40

[TF2] Hooking when a player fires a weapon
 
1 Attachment(s)
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:

Drixevel 08-27-2019 22:40

Re: [TF2] Hooking when a player fire a weapon
 
Do you mind if I build this into my API plugin? (found here: https://github.com/Drixevel/TF2-API)

Halt 08-28-2019 10:25

Re: [TF2] Hooking when a player fire a weapon
 
Neat work man :bacon!: wish I had this when I was into TF2.

ShadowMarioBR 08-28-2019 12:58

Re: [TF2] Hooking when a player fire a weapon
 
Quote:

Originally Posted by Drixevel (Post 2665031)
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! :wink:


All times are GMT -4. The time now is 23:30.

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