01.11.2010, 16:36
How to hook functions?
Well, some of you maybe wanted to hook functions sometimes. Today I came up with an idea of one "ugly hack", but I want to share it with you guys anyway. So you will need to have one filterscript that will be ALWAYS active and it should be loaded before the script you want to use hooked functions.So for example, in my gamemode I want to use GivePlayerMoney function, but everytime I use that function I want it to save that amount of money into a player variable. Now I need to hook the callback.
In the filterscript that will be always active - lets call it 'func_hook', you will have to do this:
pawn Код:
// func_hook filterscript
#include "a_samp.inc"
forward call_GivePlayerMoney(playerid, money);
public call_GivePlayerMoney(playerid, money)
{
return GivePlayerMoney(playerid, money);
}
pawn Код:
forward _ALT_GivePlayerMoney(playerid, money);public _ALT_GivePlayerMoney(playerid, money)
{
// YOU CAN DO WHATEVER HERE NOW, THIS IS THE CODE THAT GETS EXECUTED BEFORE CALLING
// GIVEPLAYERMONEY EVERYTIME YOU USE THE FUNCTION. WE'RE GONNA SET THE AMOUNT INTO PVAR
SetPVarInt(playerid, "Money", GetPVarInt(playerid, "Money") + money);
return CallRemoteFunction("call_GivePlayerMoney", "ii", playerid, money);
}
#define GivePlayerMoney _ALT_GivePlayerMoney
The code above will actually undefine the normal "GivePlayerMoney", do whatever code you put inside and then call the normal GivePlayerMoney function in another script. This may be slow solution, though I didn't clock it.