SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 14/08/2012, 04:16 PM   #1
Shetch
Gangsta
 
Shetch's Avatar
 
Join Date: Feb 2011
Posts: 526
Reputation: 40
Default What exactly does 'return 1;' do and why do we need it?

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?
__________________
Shetch is offline   Reply With Quote
Old 14/08/2012, 04:19 PM   #2
kbalor
High-roller
 
kbalor's Avatar
 
Join Date: May 2012
Location: Philippines
Posts: 1,240
Reputation: 54
Default Re: What exactly does 'return 1;' do and why do we need it?

What If you go to a tunnel and in the end it says "Dead End" What you gonna do?
__________________
kbalor is offline   Reply With Quote
Old 14/08/2012, 04:36 PM   #3
SuperViper
High-roller
 
SuperViper's Avatar
 
Join Date: Sep 2011
Posts: 1,177
Reputation: 315
Default Re: What exactly does 'return 1;' do and why do we need it?

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 Code:
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 Code:
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.
__________________
Release NameUse
FairPlayKeep the cheaters out of your server
Object Model ArrayArray of every single object model in the game
RCON PanelControl any server as you like with RCON controls
SlopeClimb up a steep hill against other players with cars falling down

I do not play on any SAMP server. Anyone with the name Ricky Phelps in-game is an imposter.
SuperViper is offline   Reply With Quote
Old 14/08/2012, 04:39 PM   #4
Admigo
High-roller
 
Admigo's Avatar
 
Join Date: Oct 2010
Location: The Netherlands
Posts: 1,840
Reputation: 297
Default Re: What exactly does 'return 1;' do and why do we need it?

Look wiki and u will see what it does.
Check the codes.
__________________
Follow me on Instagram @brandonvanommen
Admigo is offline   Reply With Quote
Old 14/08/2012, 04:41 PM   #5
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,068
Reputation: 2658
Default Re: What exactly does 'return 1;' do and why do we need it?

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.
__________________
Vince is offline   Reply With Quote
Old 14/08/2012, 04:43 PM   #6
Kirollos
Gangsta
 
Kirollos's Avatar
 
Join Date: Jun 2010
Location: Cairo, Egypt
Posts: 566
Reputation: 83
Default Re: What exactly does 'return 1;' do and why do we need it?

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

like the following example:

pawn Code:
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 Code:
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;
}
__________________
My github profile: https://github.com/Kirollos/

( ͡ ͜ʖ ͡)
Kirollos is offline   Reply With Quote
Old 14/08/2012, 05:42 PM   #7
Shetch
Gangsta
 
Shetch's Avatar
 
Join Date: Feb 2011
Posts: 526
Reputation: 40
Default Re: What exactly does 'return 1;' do and why do we need it?

Big thank you to SuperViper, Vince, kirollos!

I understand it fully now.
__________________
Shetch is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to return CMD? Gooday Scripting Help 6 02/04/2012 07:00 PM
return -1; nuriel8833 Scripting Help 2 29/02/2012 03:21 PM
return 1 or return 0? milanosie Scripting Help 3 08/02/2012 02:07 PM
return true vs return 1 +rep genesis87 Scripting Help 3 02/02/2012 08:24 AM
How to return? knackworst Scripting Help 7 25/08/2011 03:38 PM


All times are GMT. The time now is 10:09 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.