8. If, elif, else and conditional expressions
func describe_charge(charge: float) -> String:
if charge >= 80.0:
return "Fully charged."
elif charge >= 30.0:
return "Enough for one attempt."
else:
return "The battery is flat."func run() -> String:
return describe_charge(45.0)The first true branch runs. Place specific conditions before broad ones. A return can end a function early, which is useful for rejecting an impossible interaction before starting the successful sequence.
func run() -> String:
var opened: bool = true
return "Open" if opened else "Locked"A conditional expression chooses one value. Keep it short; use normal branches when a choice needs several actions or a useful explanation to the player.
