If
Returns one of two values depending on a condition. Only the chosen branch
ever evaluates; the untaken one never runs, which makes If safe to use
even when the untaken side wouldn’t otherwise be valid.
Category: Conditional Returns: matches the chosen branch; see overloads.
Signature
If(cond, then, else)
The first argument is the condition. If it isn’t already a boolean it
boolean-coerces first; see Boolean coercion.
The second is the value returned when cond is true; the third is the
value returned when cond is false.
Overloads
If always returns exactly the value of whichever branch fired, unchanged:
there’s no rounding, reformatting, or type conversion applied to it. The
table below is what the editor’s autocomplete and hover help display for
the return type, based on the types of then and else:
| Args | Returns | Notes |
|---|---|---|
(bool, int, int) | int | All-integer branches. |
(bool, decimal, decimal) | decimal | All-decimal branches. |
(bool, int, decimal) | decimal | Numeric widening. |
(bool, decimal, int) | decimal | Numeric widening. |
(bool, bool, bool) | bool | Boolean branches. |
(bool, string, string) | string | String branches. |
(bool, list, list) | list | List branches. |
(bool, number, number) | number | Numeric fallback when types can’t be statically resolved. |
(bool, any, any) | any | Catch-all. |
Examples
If(var_player_has_key, "node_open", "node_locked")
// pick a target id string by inventory state
If(var_trust_score > 50, "warm", "cold")
// string flavor switch
If(Played(node_seen_clue), 10, 0)
// contribute different amounts to a score
Clamp(var_score + If(Played(node_bonus), 5, 0), 0, 100)
// nested inside a Clamp
Notes
Lazy in both interpreted and compiled modes. Only the chosen branch evaluates; the other is never invoked. This matters most for Pick, Cycle, and Once: put one of those in the untaken branch and it simply never draws or advances, because it never runs.
The condition boolean-coerces using StoryBonsai’s standard rule; see
Expressions for the full
detail. A non-boolean condition (an int variable, a string) coerces to
true or false before If picks a branch.