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.
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.