Log in

View Full Version : [Tutorial] How to hook callbacks



Lorenc
08-24-2011, 07:44 AM
How to hook callbacks
Using the ALS method into hooking callbacks if you're making a include with a callback.

What is this?
Well, making a include may seem simple though having callbacks in the include to actually be compiled within the gamemode you're creating may seem frustrating. You'd get errors like, 'Symbol is already define, 'Callback(params)'. This tutorial will help you prevent this cause.

The ALS hooking method


/* codename: myinclude.inc */
#include <vaultmp>

public OnPlayerDeath(clientID)
{
timestamp();
new name[32]; new str[64];
GetPlayerName(clientID, name);
strformat(str, sizeof(str), false, "OnPlayerDeath: %s died\n", name);
print(str, 1, -1, 1);
return 1;
}


gamemode:


#include <vaultmp>
#include <myinclude>

public OnPlayerDeath(clientID)
{
return 1;
}

Now compiling for this would return a error:

error 021: symbol already defined: "OnPlayerJoin"

Preventing this all we do is:



/* codename: myinclude.inc */
#include <vaultmp>

//Calling the callback again.
public OnPlayerJoin(clientID)
{
timestamp();
new name[32]; new str[64];
GetPlayerName(clientID, name);
strformat(str, sizeof(str), false, "OnPlayerDeath: %s died\n", name);
print(str, 1, -1, 1);
//Return your callback
return myhook_OnPlayerJoin(clientID);
}

//if the ALS hook method is already defined:
#if defined _ALS_OnPlayerJoin
// Undefine the callback
#undef OnPlayerJoin
#else
//redefine the callback.
#define _ALS_OnPlayerJoin
#endif

//defining OnPlayerJoin with your 'onplayerjoin' that was returned.
#define OnPlayerJoin myhook_OnPlayerJoin
//forwarding it so we don't get any warnings referring to the code.
forward myhook_OnPlayerJoin(playerid);


Yeah, that's how its done. Compile and there should be no problem. Most things shall work correctly.

Tutorials still in development. Sorry for mistakes.

Southclaw
12-15-2012, 12:34 AM
Nice one Lorenc! Time to bring all the Pawn knowledge from SA:MP and put it to use here :)

Michael
01-04-2013, 02:21 PM
First tutorial I've read on this forum, it's pretty good man. First time I've seen scripting in Vault MP and as I can see it is in pawn and everything just seems so similar to SA-MP which is really good as I have experience in scripting with SA-MP so hopefully I'll be able to make a few decent scripts for VMP :)