[Tutorial] How to hook FUNCTIONS (not callbacks)
#1

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.
Reply
#2

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

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

Nice work [Odličan posao Luka ]
Reply
#5

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
Reply
#6

It works this way too
Reply
#7

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...
Reply
#8

Nice tutorial.
Reply
#9

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
Reply
#10

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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)