One Line IF Statements

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 1Snakel
:warning: Old Version Published before Godot 3 was released.

Is it possible to do something like this? If not does it have planed to support?

velocity = velocity * if (grounded): friction else: 0 

AFAIK there is no statement like this, but I this there was/is a discussion about it

timoschwarzer | 2016-07-02 07:43

If there’s something implemented like that, it should be the ternary syntax:

velocity = velocity * friction if grounded else 0

Gokudomatic2 | 2016-07-02 15:34

Dear Lord NO! Please, if anything, just implement the ternary operator.
Having the ability to inline full blown if statements ? This is the stuff TDWTF is made of.

eye776 | 2016-07-03 00:55

:bust_in_silhouette: Reply From: vnen

There’s a proposal about this, but no implementation so far. For now you still need to rely on a regular if.

Well, thanks you all. Shame, I’m not good at parsing, but i will have a look.

1Snakel | 2016-07-02 17:01

And this has now changed, see below.

CharlesMerriam | 2021-02-19 06:20

:bust_in_silhouette: Reply From: xyzzyx

According to Godot 2.1.2 release notes it is now available in the ternary syntax and working fine:

GDScript: Ternary operator (a if cond else b)
:bust_in_silhouette: Reply From: scrubswithnosleeves

The syntax is even simpler now

var raining = true

var x = "coat" if raining else "t-shirt"