[Tutorial] Callbacks and Functions : Important difference
#1

Hello there!
I see that there are quite alot (atleast too much) people whom are using callbacks as normal functions. Example:
pawn Код:
forward SendMessageToAdmins(message[]);
public SendMessageToAdmins(message[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i) || !IsPlayerAdmin(i)) continue;
        SendClientMessage(i, 0x00FF00AA, message);
    }
    return 1;
}
I know, it will just work etc, but callbacks are NOT intended to be used as functions! - You can use 'stock' for this:
pawn Код:
stock SendMessageToAdmins(message[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i) || !IsPlayerAdmin(i)) continue;
        SendClientMessage(i, 0x00FF00AA, message);
    }
    return 1;
}
Why?
Very simple: "Callbacks": "Call" and "back". They're intended to be used as .. well.. callbacks. They're intended to call at any time when something specific happens. Here's just a small list which makes it clear:
* OnGameModeInit : Call this callback when a gamemode is initialising
* OnPlayerConnect : Call this callback when a player connected
* OnVehicleMod : Called when a vehicle's been modded
~etc
So, a 'SendMessageToAdmins' -for example, again- is NOT a callback. You could make an "OnMessageToAdminsSend", like this:
pawn Код:
stock SendMessageToAdmins(message[])
{
    new pCount;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i) || !IsPlayerAdmin(i)) continue;
        pCount++;
        SendClientMessage(i, 0x00FF00AA, message);
    }
    CallLocalFunction("OnMessageToAdminsSent", "sd", message, pCount); //<-- !!!
    return 1;
}

forward OnMessageToAdminsSent(message[], ammount_of_players);
public OnMessageToAdminsSent(message[], ammount_of_players)
{
    printf("The message \"%s\" was sent to %d players (the admins)", message, ammount_of_players);
    return 1;
}
I hope that that's clear enough - If I should add anything, please say it
If there already exists something like this: Sorry for that; I searched for it (simply for 'callback' and 'callbacks'), and got only one result, and that was about hooking functions (or callbacks, can't remember very well)

Kind Regards,
Kevin aka Kwarde
Reply


Messages In This Thread
Callbacks and Functions : Important difference - by Kwarde - 22.07.2011, 23:16
Re: Callbacks and Functions : Important difference - by Cyanide - 22.07.2011, 23:34
Re: Callbacks and Functions : Important difference - by Kwarde - 22.07.2011, 23:42
Re: Callbacks and Functions : Important difference - by Cyanide - 22.07.2011, 23:47
Re: Callbacks and Functions : Important difference - by Zh3r0 - 22.07.2011, 23:49
Re: Callbacks and Functions : Important difference - by Kwarde - 23.07.2011, 08:35

Forum Jump:


Users browsing this thread: 1 Guest(s)