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?

4 Likes

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