SA-MP Forums Archive
2 operators that i don't understand - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: 2 operators that i don't understand (/showthread.php?tid=67014)



2 operators that i don't understand - ғαιιοцт - 26.02.2009

Quote:

? : e1 ? e2 : e3
results in either e2 or e3, depending on the value of e1. The
conditional expression is a compound expression with a two
part operator, “?” and “:”. Expression e2 is evaluated if e1
is logically “true”, e3 is evaluated if e1 is logically “false”.

[/quote from pawn-lang.pdf]

i don't understand what ? and : do
could anyone explain me please?


Re: 2 operators that i don't understand - ғαιιοцт - 26.02.2009

Quote:
Originally Posted by ssǝן‾ʎ
It's an inline if statement, as explained in that text.
but what do they do?
when should i use ? and when : ?


Re: 2 operators that i don't understand - Ytong - 26.02.2009

It's like:

pawn Код:
(statement) ? if-case : else-case



Re: 2 operators that i don't understand - ғαιιοцт - 26.02.2009

so this:
Код:
 y = (x == 1) ? 10 : 20;
does:
Код:
if(x == 1)
{
  y = 10;
}
else
{
  y = 20;
}
right?


Re: 2 operators that i don't understand - ғαιιοцт - 26.02.2009

if i use this:
Код:
	    if(PlayerToPoint(i, 250.0, PosBombX, PosBombY, PosBombZ))
	    {
				SetPlayerMapIcon(i, 13, PosBombX, PosBombY, PosBombZ, 23, 0);
	    }
	    else
	    {
	    	RemovePlayerMapIcon(i, 13);
	    }
it works.

but now i tried this:
Код:
	    PlayerToPoint(i, 250.0, PosBombX, PosBombY, PosBombZ) ? SetPlayerMapIcon(i, 13, PosBombX, PosBombY, PosBombZ, 23, 0) : RemovePlayerMapIcon(i, 13);
and i get this error:
Quote:

warning 215: expression has no effect

i found that here so i thought it'd work ><
http://www.lix.polytechnique.fr/~lib...nditional.html


Re: 2 operators that i don't understand - ғαιιοцт - 26.02.2009

Quote:
Originally Posted by ssǝן‾ʎ
Because that's not inline.

And don't even get me started on using PlayerToPoint!
what do you mean?
i tested if it worked ingame, and it did, i just have this warning


Quote:

215 expression has no effect
The result of the expression is apparently not stored in a variable or
used in a test. The expression or expression statement is therefore
redundant.

yes so that's why.. but i don't want to store it in a value


Re: 2 operators that i don't understand - Finn - 26.02.2009

Why don't you just use if - else in that case? It looks much better and alot easier to read than some line full of code.


Re: 2 operators that i don't understand - ғαιιοцт - 26.02.2009

Quote:
Originally Posted by Finn
Why don't you just use if - else in that case? It looks much better and alot easier to read than some line full of code.
because i want to learn how these expressions work


Re: 2 operators that i don't understand - Joske_Vermeulen - 26.02.2009

you have to use them in a function or in a return, the way you use it, it will not work.
I'll show some examples how to use it:

pawn Код:
public OnPlayerSpawn(playerid)
{
  GivePlayerMoney(playerid, (GetPlayerMoney(playerid) > 0 ? 0 : 500) );
  return 1;
}
what does it do? when the player has money (PLAYER_MONEY is bigger then 0) it gives nothing (0) else it gives 500

pawn Код:
stock IsPlayerSpawned(playerid)
return (IsPlayerConnected(playerid) && playerinfo[playerid][spawned] ? 1 : 0);
what does it do? if the player is connected and spawned it returns 1 else 0

child's play.


Re: 2 operators that i don't understand - yom - 26.02.2009

Quote:
Originally Posted by =Contador=
pawn Код:
stock IsPlayerSpawned(playerid)
return (IsPlayerConnected(playerid) && playerinfo[playerid][spawned] ? 1 : 0);
what does it do? if the player is connected and spawned it returns 1 else 0
This example isn't a good one..If it's just to check if it's either 0 or 1, you just do
pawn Код:
return IsPlayerConnected(playerid) && playerinfo[playerid][spawned];
that's the exact same thing, and may be faster to execute.


Re: 2 operators that i don't understand - Finn - 26.02.2009

Quote:
Originally Posted by 0rb
This example isn't a good one..If it's just to check if it's either 0 or 1, you just do
pawn Код:
return IsPlayerConnected(playerid) && playerinfo[playerid][spawned];
that's the exact same thing, and may be faster to execute.
Actually you don't need IsPlayerConnected() neither, because checking if the player is spawned does it itself as unconnected players cannot be spawned.

pawn Код:
return playerinfo[playerid][spawned];
LOL I know it's annoying, but it's funny how you start fixing some example scripts, he made it just so you get what ? and : are about.


Re: 2 operators that i don't understand - ғαιιοцт - 27.02.2009

so you can only use it for setting values and returning values right? (or floats, booleans, ...)


Re: 2 operators that i don't understand - Streetplaya - 27.02.2009

Quote:
Originally Posted by °ғαιιοцт°
so you can only use it for setting values and returning values right? (or floats, booleans, ...)
They are short "if-else" clauses..

You can use them if you want to check something small and use 1 expression if its right or wrong.


Re: 2 operators that i don't understand - ғαιιοцт - 27.02.2009

ok
thank you all for helping and explaining it to me ^^

I think i know now where and how to use it


Re: 2 operators that i don't understand - ғαιιοцт - 27.02.2009

Quote:
Originally Posted by ssǝן‾ʎ
ok thanks