SA-MP Forums Archive
[Tutorial] How to hook FUNCTIONS (not callbacks) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] How to hook FUNCTIONS (not callbacks) (/showthread.php?tid=187195)



How to hook FUNCTIONS (not callbacks) - Luka P. - 01.11.2010

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);
}
Now in the script where you want to hook the function, you will have to do this (please note this should be right after the "include" directives to avoid using certain function before it's hooked):
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
Conclusion:

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.


Re: How to hook FUNCTIONS (not callbacks) - Hiddos - 01.11.2010

I'm sure some people can find this useful, but you should also explain a bit more about CallRemoteFunction.


Re: How to hook FUNCTIONS (not callbacks) - Luka P. - 01.11.2010

Thanks! For those who want to know more about CallRemoteFunction, there is a wiki page for it: https://sampwiki.blast.hk/wiki/CallRemoteFunction.


Re: How to hook FUNCTIONS (not callbacks) - Aleluja - 01.11.2010

Nice work [Odličan posao Luka ]


Re: How to hook FUNCTIONS (not callbacks) - WillyP - 01.11.2010

Quote:
Originally Posted by Luka P.
Посмотреть сообщение
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
Little mistake there, forgot to put a new line


Re: How to hook FUNCTIONS (not callbacks) - Luka P. - 01.11.2010

It works this way too


Re: How to hook FUNCTIONS (not callbacks) - Kwarde - 01.11.2010

Nice tutorial.
I didn't know this yet
But about "return CallRemoteFunction("call_GivePlayerMoney", "ii", playerid, money);"
Ain't it "id" instead of "ii" ?
idk of it cares, but I always use "i" for player ID's, and "d" for money ammounts / prices, levels etc...


Re: How to hook FUNCTIONS (not callbacks) - HyperZ - 01.11.2010

Nice tutorial.


Re: How to hook FUNCTIONS (not callbacks) - Hiddos - 01.11.2010

Quote:
Originally Posted by Kwarde
Посмотреть сообщение
Nice tutorial.
I didn't know this yet
But about "return CallRemoteFunction("call_GivePlayerMoney", "ii", playerid, money);"
Ain't it "id" instead of "ii" ?
idk of it cares, but I always use "i" for player ID's, and "d" for money ammounts / prices, levels etc...
That doesn't matter, both integer and decimal are the same hehe

Edit: Luka was slower


Re: How to hook FUNCTIONS (not callbacks) - Luka P. - 01.11.2010

Quote:
Originally Posted by Kwarde
Посмотреть сообщение
Nice tutorial.
I didn't know this yet
But about "return CallRemoteFunction("call_GivePlayerMoney", "ii", playerid, money);"
Ain't it "id" instead of "ii" ?
idk of it cares, but I always use "i" for player ID's, and "d" for money ammounts / prices, levels etc...
"i" specifier is used for values that are integers, and "d" is used as alternative for "i". Simple - it's same.

Edit: Hiddos was faster