SA-MP Forums Archive
Explanation needed. - 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)
+--- Thread: Explanation needed. (/showthread.php?tid=375983)



Explanation needed. - TaLhA XIV - 08.09.2012

Hello,
I wanted to that "ternary operator",what is this?Please I need a explanation on this.
ThAnKs!


Re: Explanation needed. - Glint - 08.09.2012

Check this https://sampforum.blast.hk/showthread.php?tid=216730


Re: Explanation needed. - TaLhA XIV - 08.09.2012

Hmm can you explain them to me step by step please.ThAnKs!


Re: Explanation needed. - TaLhA XIV - 08.09.2012

Someone please explain them.


Re: Explanation needed. - SuperViper - 08.09.2012

pawn Код:
IsConnected[playerid] = (IsPlayerConnected(playerid)) ? 1 : 0;

(IsPlayerConnected(playerid)) is the if statement
? is pretty much the opening bracket
: is the else statement


For strings you'll need to enclose it in parenthesis, ex:

pawn Код:
PlayersRank[playerid] = (IsPlayerAdmin(playerid)) ? ("Administrator") : ("Player");



Re: Explanation needed. - TaLhA XIV - 08.09.2012

Hmm I see.Anymore explanations?


Re: Explanation needed. - Hiddos - 08.09.2012

Well basically it's an if-statement in an expression. For example:
pawn Код:
new val = 4;
new otherval;
if(val == 8)
{
    othervalval = 6;
}
else
{
    othervalval = 12;
}
Could also be written as:
pawn Код:
new val = 4;
new otherval;
otherval = (val == 8) ? (6) : (12);
The hooks are optional, but I usually add them to keep code readable.

In the case of if-else if-else statements:
pawn Код:
if(a == 4)
{
    b = 8;
}
else if(a == 3)
{
    b = 13;
}
else
{
    b = 2;
}
becomes...

pawn Код:
b = (a == 4) ? (8) : (a == 3) ? (13) : 2;