Wiki

trigger · check_expr

Definition

  • Supported scope:any
  • Supported target:any

Description

Evaluates a math expression and returns true when the result is non-zero.

### Parameters
- `Expression`: Any supported math expression.

### Example

check_expr = { value = 2 add = 3 greater_than = 4 }

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

check_expr 常用于需要动态计算多个变量组合结果的场景,例如判断某国工业总值、人口压力指数或自定义分数是否超过阈值,避免写多层嵌套的 check_variable。典型用途包括:根据变量加权求和来触发特殊事件、或在 available 块里验证复杂公式条件。

# 判断「变量 A 的两倍加上变量 B」是否大于 10
available = {
    check_expr = {
        value = var:my_score_var
        multiply = 2
        add = var:bonus_var
        greater_than = 10
    }
}

配合关系

  • check_variablecheck_variable 只能做简单的单值比较,当比较条件需要多步运算时改用 check_expr,两者互为替代与补充。
  • set_variable / add_to_variable:先通过这类 effect 将中间结果写入变量,再在 check_expr 中引用,可使表达式更易读、更易复用。
  • count_triggers:当需要"满足若干条件且数值运算结果也达标"时,与 count_triggers 嵌套使用,分别处理逻辑计数与数学计算两种需求。
  • custom_trigger_tooltip:包裹 check_expr 以提供玩家可读的本地化说明,避免 UI 显示空白或晦涩的内部公式。

常见坑

  1. 忘记 greater_than / less_than 等比较字段check_expr 的官方描述是"结果非零则为真",但若只写了算术字段而没有比较运算符,表达式最终返回的是计算结果本身是否非零,而非你预期的"大于某值"——务必在末尾明确写出比较条件,否则数值恰好为零时会静默返回假且不报错。
  2. 将 effect 侧的变量操作命令(如 add_to_variable)写入 trigger 块check_expr 属于 trigger,其内部只接受数学表达式参数,不能混入 effect 命令;若需要先计算再赋值,必须在 effect 块里用 set_temp_variable 等完成赋值,再在 trigger 块里引用该临时变量进行判断。

Hands-On Notes

Hands-on notes are AI-generated and checked against the vanilla command vocabulary — treat them as a starting point, not authoritative reference. The definition above is the game's own documentation.

Hands-On Usage

check_expr is commonly used in scenarios that require dynamically computing a combination of multiple variables — for example, checking whether a country's total industrial value, population pressure index, or custom score exceeds a threshold, without having to write deeply nested check_variable conditions. Typical use cases include triggering special events based on a weighted sum of variables, or validating complex formula conditions inside an available block.

# Check whether "variable A multiplied by 2, plus variable B" is greater than 10
available = {
    check_expr = {
        value = var:my_score_var
        multiply = 2
        add = var:bonus_var
        greater_than = 10
    }
}

Synergy

  • check_variable: check_variable only supports simple single-value comparisons. When a comparison requires multi-step arithmetic, switch to check_expr — the two are complementary alternatives to each other.
  • set_variable / add_to_variable: Write intermediate results into variables using these effects first, then reference them inside check_expr. This makes expressions easier to read and reuse.
  • count_triggers: When you need to satisfy both a set of logical conditions and a numeric calculation threshold, nest check_expr with count_triggers to handle logical counting and mathematical computation separately.
  • custom_trigger_tooltip: Wrap check_expr with this to provide players with a readable localized description, preventing the UI from displaying blank text or cryptic internal formulas.

Common Pitfalls

  1. Forgetting comparison fields such as greater_than / less_than: The official behavior of check_expr is "true if the result is non-zero." If you only write arithmetic fields without a comparison operator, the expression returns whether the computed result itself is non-zero — not the "greater than some value" check you intended. Always explicitly include a comparison condition at the end; otherwise, if the value happens to be zero, the trigger will silently return false with no error.
  2. Writing effect-side variable commands (such as add_to_variable) inside a trigger block: check_expr is a trigger and only accepts mathematical expression parameters internally — effect commands cannot be mixed in. If you need to compute a value and then assign it, use set_temp_variable (or similar) inside an effect block to perform the assignment first, then reference that temporary variable inside the trigger block for evaluation.