Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Ternary operator in AGK :)

Author
Message
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 8th Jan 2022 12:23 Edited at: 8th Jan 2022 12:44
I recently had someone message me privately asking if there was a ternary operator in agk.
For those not in the know; a ternary operator looks something like this:

and it's a shorthand way of saying this:


The answer is, of course, no. AppGameKit does not have a ternary operator. However, it can still be done. You just need to replace the ? with and and the : with or
So in agk the ternary operator above can be written as:


It's a useful tip for anyone that doesn't know it, so I thought I'd share it here.

[EDIT] Caveat: It only works with integers but it's still a useful tool to have in your bag
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 8th Jan 2022 13:57

You should be able to just,

canvote = age >= 18

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 8th Jan 2022 15:37 Edited at: 8th Jan 2022 15:43
That works too ... and it's more elegant.
I was just trying to make it more like a ternary operator but it clearly isn't required.

That's code blindness for you!
I'd been playing around trying to make something that performed like a ternary operator and was so happy when I got it working that I didn't notice the very obvious basics!!!

Login to post a reply

Server time is: 2024-04-20 12:43:09
Your offset time is: 2024-04-20 12:43:09