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
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 ). 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