What is the purpose of 'return' in callbacks?
#1

Hello everyone!

I've been programming PAWN for several years now, so this question may surprise you, the question is why do we return a value when we use callbacks? I understand why we use it in functions, to return a value obviously. I have provided two examples below of when I would and wouldn't use them in functions.

pawn Код:
// When I WOULD use 'return' in a function

stock AddNumbers(num1, num2)
{
    return num1 + num2;
}

// When I WOULDN'T use 'return' in a function

new bool:someBool;

stock ToggleBool()
{
    if (someBool)
    {
        someBool = false;
    }
    else
    {
        someBool = true;
    }
}
Now what I don't understand is why we use them in callbacks, it's not like we're using them to perform calculations, right?

I mean, what's the difference between this...

pawn Код:
public OnGameModeInit()
{
    print("Script has been initialised!");
    return 1;
}
...this...

pawn Код:
public OnGameModeInit()
{
    print("Script has been initialised!");
    return 0;
}
...or this?

pawn Код:
public OnGameModeInit()
{
    print("Script has been initialised!");
}
When I create callbacks I always feel the need to add 'return 1' at the end, since every other callback has them.

pawn Код:
forward MyCallback();
public MyCallback()
{
    print("Hello there!");
    return 1;
}
Can anyone explain why we need to do this? And if we don't actually have to do it, could you please explain why the standard callbacks return a value? Thank you very much.
Reply
#2

Same question was asked a while back. For default callbacks, it tells the server whether it should process the same callback in other (filter)scripts.
Reply
#3

In custom callbacks is not necessary to return values and it's not actually necessary to return a value to some of the callbacks (by samp) either.

You may see in SA-MP Wiki in some callbacks to say "This callback does not handle returns." and some other say what happens if we return a specific value. Let's take an example of OnPlayerCommandText callback. SA-MP Wiki says:
Quote:

Returning 0 informs the server that the command hasn't been processed by this script. OnPlayerCommandText will be called in other scripts until one returns 1.

Returning 0 in some callbacks stops those callbacks from being called in other modes such as filterscripts etc.

Another example is OnPlayerText callback and returning 0 will disable the default chat.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
Same question was asked a while back. For default callbacks, it tells the server whether it should process the same callback in other (filter)scripts.
So there isn't any need for it in custom callbacks?

And whenever a default callback is called it ALWAYS calls the one within the gamemode script? I've noticed that returning the value 0 in the 'OnPlayerText' callback will stop the chat all together to allow a custom chat format. I guess this is the process?:

Код:
> Player talks
> 'OnPlayerText' is called in the gamemode script
> Code gets executed
> return 1:
	> Processes the message in an internal script
	> Continues to other filterscripts
> return 0:
	> Callback stops completely, not called anywhere else
EDIT: Time limit not allowing me to double post:

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
In custom callbacks is not necessary to return values and it's not actually necessary to return a value to some of the callbacks (by samp) either.

You may see in SA-MP Wiki in some callbacks to say "This callback does not handle returns." and some other say what happens if we return a specific value. Let's take an example of OnPlayerCommandText callback. SA-MP Wiki says:
Returning 0 in some callbacks stops those callbacks from being called in other modes such as filterscripts etc.

Another example is OnPlayerText callback and returning 0 will disable the default chat.
Ahh okay that makes sense, check the above post for my assumption on how 'OnPlayerText' works too.
Reply
#5

I suggest you try "return 0;" and "return 1;" in OnDialogResponse in your filterscript (as stated in your server.cfg file) and try to process a dialog that's processed in the second filterscript or your gamemode.

Filterscript 1:
pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    return 0;
}
Gamemode:
pawn Код:
//Somewhere in your code:
ShowPlayerDialog(playerid, 1, DIALOG_STYLE_LIST, "Title:", "Option 1\nOption 2", "OK", "");

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch (dialogid)
    {
        case 1: if (response == 1) printf("You've clicked the OK button");
    }

    return 1;
}
Now execute the ShowPlayerDialog in your gamemode and click the OK button.
You'll see that your server console will print "You've clicked the OK button".

Change the "return 0;" to "return 1;" in your filterscript and execute the ShowPlayerDialog again.
When you click on the OK button now, you'll NOT see the message appear in your server console.
You'll think your gamemode has errors/bugs, but the fault lies within your filterscript.
Why?

When you process a dialog's responses in your server, the server will first look for the dialogid "1" in your filterscripts.
It doesn't find it there as there is no code to process it.
But it finds "return 1;" in there and the server now thinks you've processed the dialog in your filterscript.
The server won't proceed to your other filterscripts and gamemode as returning 1 in OnDialogResponse actually means that the dialog has been processed and won't look any further.

Now, if you put "return 0;" again in your filterscript, it's working again for that dialog in your gamemode.
Returning 0 means that dialogid "1" isn't processed in your filterscript, and the server will call the callback in your other filterscripts and finally in your gamemode.

After seeing "return 0;" in your filterscript (and there are no other filterscripts in this example), the server will move on to your gamemode and call OnDialogResponse there.
In your gamemode, it finds dialogid 1, processes it and prints the message properly.

In short:
"return 1;" -> stop processing OnDialogResponse (don't move on to the next script)
"return 0;" -> go to the next script and call OnDialogResponse



Some other callbacks have different meanings for "return 0;" and "return 1;".
As stated above, OnPlayerText works the other way around:
"return 1;" -> go to the next script and call OnPlayerText
"return 0;" -> stop processing OnPlayerText (don't move on to the next script)



In the gamemode however, you can return anything you want in that callback, as the gamemode is the last script in line to be processed anyway. So it doesn't matter if you allow the server to look further, as there are no other scripts to call after the gamemode.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)