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:

ArgsReturnsNotes
(bool, int, int)intAll-integer branches.
(bool, decimal, decimal)decimalAll-decimal branches.
(bool, int, decimal)decimalNumeric widening.
(bool, decimal, int)decimalNumeric widening.
(bool, bool, bool)boolBoolean branches.
(bool, string, string)stringString branches.
(bool, list, list)listList branches.
(bool, number, number)numberNumeric fallback when types can’t be statically resolved.
(bool, any, any)anyCatch-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.

Docs last synced: 2026-07-08
Screenshot viewer