Conditional Branches
Use Condition to choose an action list at runtime.
import { Condition } from "narraleaf-react";
scene.action([
Condition.If(player.equals("favoriteTea", "green"), [
alice.say("I made green tea."),
]).Else([
alice.say("I made coffee."),
]),
]);A condition may have one If, any number of ElseIf branches, and one final Else.
scene.action([
Condition.If(player.evaluate("coins", (coins) => coins >= 100), [
alice.say("You can afford the rare book."),
])
.ElseIf(player.evaluate("coins", (coins) => coins >= 20), [
alice.say("You can afford the map."),
])
.Else([
alice.say("Come back when you have more coins."),
]),
]);Read multiple values
Pass a function when the condition depends on more than one value. The context provides $() for registered namespaces.
scene.action([
Condition.If(({ $ }) => {
const state = $("persistent:player");
return state.get("hasKey") && state.get("coins") >= 10;
}, [
alice.say("The gate opens."),
]),
]);For a single field, prefer equals, isTrue, isFalse, or evaluate; they are shorter and keep the type of the value.
Branch order is fixed. Call ElseIf() after If(), and Else() last. Adding another branch after Else() throws an error.