Question about "return"
#1

Hello guys, please can someone explain me when to use return 1; or return 0; Becouse I dont found any tutorial or something and I understand when to use it. For example why every callback (except OnPlayerCommandText) have return 1? What will happen if i set it to 0; ? And for example when I create new public with forward, how can I know so here should be return 1 or 0?
Reply
#2

Basicly
pawn Код:
return 1;
means the script will see it as true and will execute it, if it returns false
pawn Код:
return 0;
it means the script will ignore it as far as I know. For example take OnPlayerUpdate, if it returns 0 the server wont update the player because the script is not executing it.
Theres probably someone that can explain this better, Good luck
Reply
#3

Quote:

return stops a function and goes back to the point in code which called the function in the first place

Quote:

...You can also use return to return a value

(example below)

pawn Код:
GetServerPlayers()
{
 new players;
 for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
 {
  if(IsPlayerConnected(playerid))
   players ++;
 }
 return players; //Returns the amount of connected players
}

if(GetServerPlayers() > 0) // GetServerPlayers() = Amount of connected players
 SendClientMessageToAll(-1, "There are players online");
Source: https://sampwiki.blast.hk/wiki/Control_Structures#return

Beyond that some callbacks and functions handle returns. One function example is CreateVehicle:

Quote:

The vehicle ID of the vehicle created (1 - MAX_VEHICLES).
INVALID_VEHICLE_ID (65535) if vehicle was not created (vehicle limit reached or invalid vehicle model ID passed).

pawn Код:
vehicleid = CreateVehicle(modelid, x, y, z, angle, color1, color2, respawn_delay);
if(vehicleid == INVALID_VEHICLE_ID)
 print("Vehicle could not be created.");
else
 printf("Vehicle ID %i has been created.", vehicleid);
Reply
#4

And why for example:

pawn Код:
PublicGMX ()
{
    SendRconCommand("GMX");
    return false;
}
Reply
#5

Quote:
Originally Posted by Riwerry
Посмотреть сообщение
And why for example:

pawn Код:
PublicGMX ()
{
    SendRconCommand("GMX");
    return false;
}
You could use "return;" instead. No difference in this case.
Reply
#6

Yeah i know so its same but why is here return 0 instead of 1? what will hapen if it is return 1?
Reply
#7

The best way for me to try to explain this, with the knowledge I have of scripting, is in the case of OnDialogResponse. If you had one part where you were customizing what would happen in a /getgun command where a dialog popped up and gave you different choices, but you wanted a /spawncar command under that same public, you wouldn't want to mix them obviously. So after you were all said and done with /getgun's dialog, you'd add return 1; then start your /spawncar's dialog. I believe return 0; stops the script? Although I'm not sure.
Reply
#8

Unless the return value ("false") of PublicGMX gets assigned or used somewhere (see below for example), it doesn't matter. Some prefer to return 0 to indicate something has failed.

pawn Код:
if(PublicGMX() == true)
{
    // success
}
else
{
    // fail
}
Return values in the default SA-MP callbacks usually control whether or not the same callback should also be called in filterscripts that are being loaded. In OnPlayerCommandText, returning 1 means that the command was found and that no further processing should occur.

In pretty much all other callbacks, the opposite is true. Returning 1 means that additional code in filterscripts will be executed. Returning 0 will prevent just that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)