Ternary Operator
#1

Hello, I've added shell system to my gamemode which drops a "shell" when you fire a weapon. Everything works well except a ternany operator.
Here's the code:
Code:
                        new time = (tickcount() - ShellInfo[id][ShellTick]) / 1000;
			
			if (time < 5)
				return SendClientMessageEx(playerid, COLOR_LIGHTRED, "The shell is too hot, you must wait %d more %s before picking it up.", 5 - time, (time == 1) ? ("second") : ("seconds"));
I want to tell the player to wait "%d more seconds" if the seconds (variable "time") are above 1 and "%d more second" if the amount of seconds is 1. It tells me "seconds" every time I need to wait 5, 3, 2 or 1 seconds and "second" when I need to wait 4 seconds . As you can see I'm doing 5 - time, which equals to 4, but if I won't do that then the time is 1. Maybe that's why it says "second" for 4 (because if it's 4 then the "time" variable equals to 1)? Because nothing seems to be wrong there.
Reply
#2

I'm dumb, that's what I had to do:
Code:
(5 - time == 1) ? ("second") : ("seconds")
EDIT: And I just realized that I shouldn't have bumped the thread, but just edit it.
Reply
#3

I don't know why the ternary operator even exists when a simple if-then and else beats it in performance.
Reply
#4

Precisely for small things like this that would otherwise require copying the same line or introducing an extra variable.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)