What exactly does 'return 1;' do and why do we need it?
#1

I know this is kind of a beginner question, and I am actually pretty embarrassed to ask this...
Just like the title says, what exactly does 'return 1;' do and why do we need it?
Reply
#2

What If you go to a tunnel and in the end it says "Dead End" What you gonna do?
Reply
#3

In some callbacks, it's used to determine if an action should be completed or prevented. Take OnPlayerUpdate as an example, if you return 0 in OnPlayerUpdate, it'll prevent the new data sent from being synced to other players, where as if you return 1, it'll sync to other players.

In functions, it's usually used to return a certain value such as a player's admin level for example.

pawn Код:
GetAdminLevel(playerid)
{
    return playersAdminLevel[playerid];
}
You don't need return 1 here because you're trying to make this function return the player's admin level.

Return 1 isn't something you need everywhere, but you should have it in your functions and callbacks because lets say you're making a command and you feel lazy so you want to do something like

pawn Код:
return SendClientMessage(playerid, -1, "Hello world...");
This will run all of the code inside of the SendClientMessage function and it will return the value which is returned from the function SendClientMessage, which would be 1.
Reply
#4

Look wiki and u will see what it does.
Check the codes.
Reply
#5

Functions only need to return a value if that value is being used somewhere. Returning 0 or 1 in the default callbacks will tell the script whether or not to call the callback in other scripts.

If the return value is of no importance then the return statement can be omitted. Alternatively, if you have conditions in your function you may put return without any value.
Reply
#6

return means to return a value in a function or w/e

like the following example:

pawn Код:
new playernub;

public OnPlayerConnect(playerid)
{
    playernub = GetPlayerNub(playerid); // GetPlayerNub will now return rand (0 or 1 , according to the random function)

    printf("GetPlayerNub function on playerid %i result is: %i", playerid, playernub);
    return 1;
}

stock GetPlayerNub(playerid)
{
    new rand = random(1);
    return rand; // will return 1 or 0 (according to the random function)
}
it also means to stop continuing from reading the other part of the script.

like the following

pawn Код:
public OnPlayerConnect(playerid)
{
    if(IsPlayerNPC(playerid)) return 1; // checks if the player that connected is an NPC, the script will stop reading OnPlayerConnect.

    ShowPlayerLogin(playerid, login, blablabl);
    return 1;
}
Reply
#7

Big thank you to SuperViper, Vince, kirollos!

I understand it fully now.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)