SA-MP Forums Archive
Ternary Operator - 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: Ternary Operator (/showthread.php?tid=602744)



Ternary Operator - AMouldyLemon - 12.03.2016

Code:
new gender[16];
new IsPlayerMale = (IsPlayerMale(playerid)) ? format(gender, sizeof(gender), "Male") : format(gender, sizeof(gender), "Female"); // 429
Code:
(429) : error 012: invalid function call, not a valid address
(429) : warning 215: expression has no effect
(429) : error 001: expected token: ";", but found ")"
(429) : error 029: invalid expression, assumed zero
(429) : fatal error 107: too many error messages on one line
Not sure why this won't work. First time using the Ternary Operator.


Re: Ternary Operator - Catalyst- - 12.03.2016

The problem isn't with the ternary operator. You have given the variable the same name as the function.

pawn Code:
new isMale = (IsPlayerMale(playerid)) ? format(gender, sizeof(gender), "Male") : format(gender, sizeof(gender), "Female"); // 429
isMale won't actually contain a useful value; it will just be whatever format returns. If you need to use format, then in this case you may as well just use a normal if/else statement.

Otherwise, this is how you use strings in a ternary operator:
pawn Code:
new gender[7];
gender = IsPlayerMale(playerid) ? ("Male") : ("Female");



Re: Ternary Operator - AMouldyLemon - 12.03.2016

Ah, thank you.