SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 27/07/2015, 09:50 PM   #1
Nin9r
Huge Clucker
 
Join Date: Feb 2015
Posts: 331
Reputation: 3
Question How to use return 1/0 ?

Hi there!

I can't understand when i have to use return 1 or return 0 in a function.

For example, which is good?


Code:
if(listitem == 0)
			{
				if(PlayerData[playerid][pLocateCar] == 1)
				{
					format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
					SendClientMessage(playerid, COLOR_WHITE, szMessage);
				}
			}
or

Code:
if(listitem == 0)
			{
				if(PlayerData[playerid][pLocateCar] == 1)
				{
					format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
					SendClientMessage(playerid, COLOR_WHITE, szMessage);
				}
                                return 1;
			}
When and why?
Nin9r is offline   Reply With Quote
Old 27/07/2015, 09:56 PM   #2
Mencent
High-roller
 
Mencent's Avatar
 
Join Date: Mar 2015
Location: Lower Saxony (Niedersachsen)
Posts: 1,311
Reputation: 92
Default AW: How to use return 1/0 ?

Hello!

It's both the same only it return one times 1 and one times 0.
But it is both the same.

This is better as you don't use return.
PHP Code:
if(listitem == 0)
{
    if(
PlayerData[playerid][pLocateCar] == 1)
    {
        
format(szMessage,sizeof szMessage,"Your car (Model: %s) has been respawned!",VehicleNames[PlayerData[playerid][pCarModel1]-400]);
        
SendClientMessage(playerid,COLOR_WHITE,szMessage);
        return 
1;
    }

__________________
Best wishes,
Mencent

(Sorry for my bad english, I hope you can understand this!)
Mencent is offline   Reply With Quote
Old 27/07/2015, 09:57 PM   #3
Rodri99
Banned
 
Join Date: Dec 2014
Location: Italy
Posts: 152
Reputation: 4
Default Re: How to use return 1/0 ?

if(listitem == 0){
if(PlayerData[playerid][pLocateCar] == 1)
{
format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
SendClientMessage(playerid, COLOR_WHITE, szMessage);
return 1;
}
}
Rodri99 is offline   Reply With Quote
Old 27/07/2015, 09:59 PM   #4
Nin9r
Huge Clucker
 
Join Date: Feb 2015
Posts: 331
Reputation: 3
Default Re: AW: How to use return 1/0 ?

Quote:
Originally Posted by Mencent View Post
Hello!

It's both the same only it return one times 1 and one times 0.
But it is both the same.

This is better as you don't use return.
PHP Code:
if(listitem == 0)
{
    if(
PlayerData[playerid][pLocateCar] == 1)
    {
        
format(szMessage,sizeof szMessage,"Your car (Model: %s) has been respawned!",VehicleNames[PlayerData[playerid][pCarModel1]-400]);
        
SendClientMessage(playerid,COLOR_WHITE,szMessage);
        return 
1;
    }

1. Is absolutely necessary to use a return value there? (Why?)
2. if i will forget to use a return value, is it a problem ? It can be an issue for bad script working?
Nin9r is offline   Reply With Quote
Old 27/07/2015, 10:00 PM   #5
TenTen
Big Clucker
 
TenTen's Avatar
 
Join Date: Jul 2015
Posts: 196
Reputation: 4
Default Re: How to use return 1/0 ?

Code:
if(listitem == 0){
if(PlayerData[playerid][pLocateCar] == 1)
{
format(szMessage, sizeof(szMessage), "Your car (Model: %s) has been respawned!", VehicleNames[PlayerData[playerid][pCarModel1] - 400]);
SendClientMessage(playerid, COLOR_WHITE, szMessage);
return 1;
}
}
TenTen is offline   Reply With Quote
Old 27/07/2015, 10:03 PM   #6
Nin9r
Huge Clucker
 
Join Date: Feb 2015
Posts: 331
Reputation: 3
Default Re: How to use return 1/0 ?

1. Is absolutely necessary to use a return value there? (Why?)
2. if i will forget to use a return value, is it a problem ? It can be an issue for bad script working?
Nin9r is offline   Reply With Quote
Old 27/07/2015, 10:05 PM   #7
Abagail
High-roller
 
Join Date: Dec 2013
Location: GetPlayerPos(playerid, pos); Scripter/Mapper.
Posts: 3,133
Reputation: 445
Default Re: How to use return 1/0 ?

Returning a value is usually optional (though it will usually give a warning), though it can be useful and or needed in some situations. When simply responding to a callback typically returning 1 would be practical unless the callback does different things(such as a OnTakeDamage function not giving damage if 0 is returned) when other things are returned.

Though for functions returns can sometimes be useful & or needed. For example:
pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate) {
     if(newstate == PLAYER_STATE_DRIVER && IsACopVehicle(GetPlayerVehicleID(playerid)) {
           if(!IsACop(playerid)) return RemovePlayerFromVehicle(playerid);
     }

     return 1;
}

In this case, IsACop and IsACopVehicle return different values. This allows you to check if(if it returns 1) the player is a cop and if it returns 0 the player isn't a cop.

Also 1/0 aren't the only values you can return. You can return strings, and other integer values as well. NOTE: Due to an issue with the compiler directly returning a string will crash the compiler(e.g: return "yes". This works in old compiler versions and also if you're using Zeex's compiler patches(I believe).
Abagail is offline   Reply With Quote
Old 27/07/2015, 10:07 PM   #8
Mencent
High-roller
 
Mencent's Avatar
 
Join Date: Mar 2015
Location: Lower Saxony (Niedersachsen)
Posts: 1,311
Reputation: 92
Default AW: How to use return 1/0 ?

1.) It is better, otherwise the codes continue also if you don't want it.

2.) No, it is not a problem but the codes will continue also if you don't want it.
__________________
Best wishes,
Mencent

(Sorry for my bad english, I hope you can understand this!)
Mencent is offline   Reply With Quote
Old 27/07/2015, 10:10 PM   #9
Nin9r
Huge Clucker
 
Join Date: Feb 2015
Posts: 331
Reputation: 3
Default Re: How to use return 1/0 ?

Quote:
Originally Posted by Abagail View Post
Returning a value is usually optional (though it will usually give a warning), though it can be useful and or needed in some situations. When simply responding to a callback typically returning 1 would be practical unless the callback does different things(such as a OnTakeDamage function not giving damage if 0 is returned) when other things are returned.

Though for functions returns can sometimes be useful & or needed. For example:
pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate) {
     if(newstate == PLAYER_STATE_DRIVER && IsACopVehicle(GetPlayerVehicleID(playerid)) {
           if(!IsACop(playerid)) return RemovePlayerFromVehicle(playerid);
     }

     return 1;
}

In this case, IsACop and IsACopVehicle return different values. This allows you to check if(if it returns 1) the player is a cop and if it returns 0 the player isn't a cop.

Also 1/0 aren't the only values you can return. You can return strings, and other integer values as well. NOTE: Due to an issue with the compiler directly returning a string will crash the compiler(e.g: return "yes". This works in old compiler versions and also if you're using Zeex's compiler patches(I believe).

Thank you, but in this case is absolutely necessary?

Code:
if(listitem == 0) 
{ 
    if(PlayerData[playerid][pLocateCar] == 1) 
    { 
        format(szMessage,sizeof szMessage,"Your car (Model: %s) has been respawned!",VehicleNames[PlayerData[playerid][pCarModel1]-400]); 
        SendClientMessage(playerid,COLOR_WHITE,szMessage); 
        return 1; 
    } 
}
Nin9r is offline   Reply With Quote
Old 27/07/2015, 10:13 PM   #10
!damo!spiderman
Huge Clucker
 
!damo!spiderman's Avatar
 
Join Date: Oct 2006
Posts: 244
Reputation: 160
Default Re: How to use return 1/0 ?

The return statement is used to return a value back from a function, not necessarily 1 or 0. For instance

PHP Code:
function adda){
     return 
b;
}

new 
result add5); // result is now 11 
If you're meaning sa-mps public functions/callbacks like OnPlayerSpawn etc then it really depends on the callback. Sometimes returning will cancel the effects of the the callback. For instance https://wiki.sa-mp.com/wiki/OnPlayerWeaponShot states that returning 0 will cause the bullet to do no damage, but returning 1 will allow it to damage as usual. Other callbacks such as OnCommandText returning 0 will tell it that you did not handle the command and will continue to on into other filterscripts until it receives return 1, if not the command was not handle and it will notify the player of Command does not exist or whatever the message is. Other callbacks the return value doesn't matter. Check wiki if you're unsure
__________________
!damo!spiderman 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
What's the diffrences between Return True and Return False davidstyle1125 Scripting Help 4 09/12/2014 08:34 PM
return true vs return 1 +rep genesis87 Scripting Help 3 02/02/2012 08:24 AM
When do I return? NiiRV4N4 Help Archive 9 13/06/2010 04:14 PM


All times are GMT. The time now is 12:15 PM.


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