NarraLeaf

条件分支

通过 If、ElseIf、Else 实现剧情分支的 `Condition` 类

Condition 用于在剧本中创建条件分支

Condition
    .If((ctx) => {
        return // 你的条件
    }, [
        character1.say`条件为真`,
    ])
    .Else([
        character1.say`条件为假`,
    ])

通常配合 Persistent 进行标志位或变量的判断:

type PersistentData = {
    coin: number;
    flag: boolean;
    hobby: string;
};

const persis = new Persistent<PersistentData>("persis", {
    coin: 0,
    flag: false,
    hobby: "coding",
});

// 检查金币是否不足
Condition
    .If(persis.evaluate("coin", (coin) => coin < 50), [
        character1.say("你没有足够的金币"),
    ])

// 检查标志位
Condition
    .If(persis.isTrue("flag"), [
        character1.say("你有这个标志"),
    ])

// 检查变量值
Condition
    .If(persis.equals("hobby", "coding"), [
        character1.say("你喜欢编程"),
    ])

注意: 方法必须按正确顺序调用,否则会抛出错误。例如不能在 If 之前调用 ElseIf

静态方法

If

  • condition: Lambda | LambdaHandler<boolean> - 条件表达式,返回 true 时执行后续操作。参见 LambdaLambdaHandler
  • action: ActionStatements - ActionStatements
Condition.If(({ storable }) => {
    return ($("name").get("coin") || 0) >= 10;
}, [
    character1.say("你有足够的钱")
])

公共方法

constructor

链式方法

ElseIf

  • condition: Lambda | LambdaHandler<boolean>
  • action: ActionStatements - ActionStatements

Else

本页目录