Callbacks and Functions : Important difference -
Kwarde - 22.07.2011
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
Re: Callbacks and Functions : Important difference -
Cyanide - 22.07.2011
I understand this already however the way you explained it wasn't too good. For example:
Quote:
They're intended to call at any time when something specific happens.
|
You can also use functions when something specific happens. Also, the code you put as a example for OnMessageToAdminsSend is terribly coded, for several reasons:
- You're using CallLocalFunction when you can simply just call it normally.
- SendMessageToAdmins(message) is your code, you can only send integers to all admins.
- SendMessageToAdmins will print errors because you're tag mismatching.
You should of also explained why you use "stock" before you declare a function before people begin using it and not knowing what it exactly means. Tutorials are meant to be released if you know exactly what you're doing.
Check this thread out!
Re: Callbacks and Functions : Important difference -
Kwarde - 22.07.2011
1. Sorry, I'm used to use CallLocalFunction
2. I know, thanks for reporting. Didn't notice that yet, small mistake. (Only once)
3. Because of 2 right? If not, wth

[[4. Just noticed that I have one 'On...sent' and one 'On...send'
Okay, just decided that I won't write tutorials anymore after 22:00 (10PM ; GMT+1)
Fixed.
I know what I'm doing, I just made a few mistakes. Sorry 'bout that.
Re: Callbacks and Functions : Important difference -
Cyanide - 22.07.2011
Quote:
Originally Posted by Kwarde
1. Sorry, I'm used to use CallLocalFunction
2. I know, thanks for reporting. Didn't notice that yet
3. Because of 2 right? If not, wth 
[[4. Just noticed that I have one 'On...sent' and one 'On...send'
Okay, just decided that I won't write tutorials anymore after 22:00 (10PM ; GMT+1)
Now gonna fix it
|
It's all cool. Everyone is learning still. Here's a edit of your code. ( I'm a bit PAWN rusty. You might notice some flaws ) - Also, good job trying at least Kwarde.
pawn Код:
sendMessageToAdmins( message[ ] )
{
static
count;
for( new z; z != MAX_PLAYERS; ++ z )
{
if( !IsPlayerConnected( i ) || ( !IsPlayerAdmin( i ) ) continue;
count ++;
SendClientMessage( i, 0x00FF00AA, message );
}
onMessageToAdminsSent( message, count );
}
onMessageToAdminsSent( message[ ], player_count )
{
printf( "The message \"%s\" was sent to %i players (the admins)", message, player_count );
return true;
}
Re: Callbacks and Functions : Important difference -
Zh3r0 - 22.07.2011
I was so about to tell you Cyanide that you typed the function with a capital 'O' but you noticed faster!! xD
@Kwarde - Keep up the good work.
PS: Never make tutorials when: It's late, you are drunk, getting laid.
Re: Callbacks and Functions : Important difference -
Kwarde - 23.07.2011
Quote:
PS: Never make tutorials when: It's late, (.. Not applicable ..)
|
Haha managed that out myself too

However, what about the 'O'; I don't see any problems with it.