$if / $elseIf / $else / $endif
Conditional branching — executes blocks of code based on runtime condition evaluation.
$if[condition].. $elseIf[condition].. $else.. $endif
$if / $elseIf / $else / $endif — Conditional Branching
The $if family of tokens provides conditional branching in BDFD scripts. These are structural tokens handled at the BDFD parser level, not dispatched as regular function calls. They allow you to execute different blocks of commands based on runtime conditions.
How It Works
When the parser encounters a $if[condition] token, it evaluates the condition. If the condition is true, it executes the block immediately following $if, up to the next $elseIf, $else, or $endif. If the condition is false, it skips forward:
- It checks any subsequent
$elseIf[condition]blocks in order. - If no condition matches, the optional
$elseblock is executed. $endifis always required to close the if-block, even if only$ifand$elseare used.
Condition Syntax
Conditions are BDFD expressions. They are not traditional programming language booleans — they are evaluated at runtime by the BDFD expression engine.
Supported Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | $getUserVar[score]==100 |
!= |
Not equal to | $getUserVar[name]!=Guest |
> |
Greater than | $getUserVar[coins]>0 |
< |
Less than | $getUserVar[hp]<25 |
>= |
Greater or equal | $getUserVar[level]>=10 |
<= |
Less or equal | $getUserVar[wins]<=3 |
Logical Operators
Combine multiple conditions with $and and $or:
$if[$and[$getUserVar[gold]>=100;$getUserVar[rank]>=5]==true]
$if[$or[$checkContains[$message;ping];$checkContains[$message;pong]]==true]
Inline Check Functions
Use $checkCondition or $checkContains inside an $if:
$if[$checkCondition[>=;$getUserVar[age];18]==true]
$if[$checkContains[$message;admin]==true]
Structural Rules
$endifis mandatory — every$ifmust have exactly one$endif.$elseis optional — at most one per$ifblock.$elseIfis optional — you can chain as many as you need.- Nesting is supported — you can place
$if...$endifinside another$ifblock. - Conditions are evaluated sequentially — the first matching branch wins; subsequent branches are skipped.
Common Pitfalls
- Forgetting
$endifcauses a parse error. - Using
=instead of==for equality — BDFD requires double equals. - Comparing strings with numeric operators — make sure the value type matches the comparison intent.