Ternary operator

Something that I would really like to see in GDevelop is the ternary operator. For example, if I want to offset a ghost to always be behind the player, I would do today something like

if(player is flipped) {
    Set position of ghost to player.X() + 15
if(player is not flipped) {
    Set position of ghost to player.X() - 15

That is very tedious and a lot of code :confused:
That’s where the ternary would help, we could have code that looks more like this:

Set position of ghost to player.X() + (15 if player is flipped else -15)

I think the syntax (<expressionIfTrue> if <condition> else <expressionIfFalse>) is pretty readable (and may be stolen a bit from python, with extra parenthesis for better readability :smirk:). This syntax is also better for autocomplete purposes, as the autocomplete can be aware that you will write a condition if you wrote if before, but wouldn’t if the condition comes before the operator like in the more standard <condition> ? <expressionIfTrue> : <expressionIfFalse>.

The main problem lies in object selection I think, would the object be selectionned or not if a non free condition is called?


I’d like to bump this as it is an important feature to me.

As it is in an expression, I don’t think object selection should apply here.

I would like comments on this if possible, I am not 100% convinced this is the best and it probably be a bit hard to implement, as AFAIK the <expressionIfTrue> would have already been parsed and it wouldn’t be easy to make it become a child node of the ternary, and a single character like the ? in x ? y : z syntax is easier to check for than the multiple characters of the if of the y if x else z syntax.

I’m having a real hard time parsing how this works in the current event sheet, or how it could.

Would this basically be it’s own type of event, similar to while?

It is an operator, it would be part of the expressions syntax, like this for example:
"Level" + ("Bonus" if CompareTwoValue(RandomInRange(), "=", 5) else "2")

To make sure I’m connecting, you’re not saying “if blah = 5 do this, otherwise do this”, you’re saying “If blah =5, treat as true, otherwise if it = 2, treat as true”

Is that accurate?

No, it is a condition in the form of an Expression. You can think of it as an events based expression written in one line:
(3 if Always() else 5)

if(Always()) {
    Set return value to 3
if (not Always()) {
    Set return value to 5

The example in my previous post would be an Expression that returns “Level2” 4/5 times and “LevelBonus” 1/5 times.

Would this only be for numbers or other expressions, then?

If so, I kind of get what you mean now. I’d think we’d need a much more specific identifier to avoid people having to learn when spaces do/do not work, etc.

Maybe even just mirroring how it’s done in something like Excel, with an ifElse(Equation,resultontrue,resultonelse)?

I think it is a good idea but the syntax should be more simple and shorter otherwise it does not help much if we need to type/read a long expression. How about:

“Level” + (RandomInRange(0,5) < 5) ? “2” : “Bonus”

Where, the ternary operator is the ? mark and so the compiler knows to look for a condition in between () on the left side of the ? and look for 2 values separated by a : to return if the condition on the left was true and if it was false.

If it has to be a more traditional expression then how about:
“Level” + Ternary(RandomInRange(0,5) < 5, “2”, “Bonus”)

Ideally, it should work with any value , string, number, expression, otherwise no point using it. The entire point of ternary operator is to offer a quick and short solution to compare something and return a value in case of true and false.

1 Like