Wiki

trigger · subtract_from_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Subtracts a value, a variable, or a [math expression](script_math_expression.md) from a temporary variable.

### Examples

subtract_from_temp_variable = { num_dogs = 5 } subtract_from_temp_variable = { num_dogs = { value = num_cats multiply = 2 } }

实战 · 配合 · 坑

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

实战用法

subtract_from_temp_variable 常见于需要在条件块内对临时变量做减法运算的场景,例如计算某国资源差值后再与阈值比较,或在复杂的数组遍历中实时更新累加器。典型用法是先用 set_temp_variable 初始化一个临时变量,再通过本命令减去某个固定值或另一个变量,最后用 check_variable 判断结果是否满足条件。

# 判断某国工厂总数减去目标值后是否仍大于 0
if = {
    limit = {
        set_temp_variable = { var = factory_diff value = num_civilian_factories }
        subtract_from_temp_variable = { var = factory_diff value = 10 }
        check_variable = { factory_diff > 0 }
    }
    # 满足条件时执行的 effect ...
}

配合关系

  • [set_temp_variable](/wiki/trigger/set_temp_variable):几乎总是在本命令之前调用,用于初始化被减数,否则临时变量为未定义状态,减法结果不可预期。
  • [check_variable](/wiki/trigger/check_variable):减法完成后立即用它对结果做大小比较,是整个"计算→判断"流程的最后一环。
  • [add_to_temp_variable](/wiki/trigger/add_to_temp_variable):与本命令搭配实现加减混合运算,常见于多步数学推导(如先累加再扣除惩罚值)。
  • [multiply_temp_variable](/wiki/trigger/multiply_temp_variable):在需要先缩放再减去偏移量的场景下紧接本命令使用,例如计算百分比差值。

常见坑

  1. 在 trigger 块中误用 effect 版本subtract_from_temp_variable 同时存在于 trigger 和 effect 白名单,但两者语义不同——trigger 版本只做计算并返回真值,不会持久化任何状态;若把它写进 effect 块,游戏实际执行的是 effect 版本,行为看似相同却容易造成逻辑混淆,调试时难以区分。
  2. 临时变量跨 scope 失效temp_variable 的生命周期绑定当前求值上下文,一旦通过 any_countryany_state 等作用域切换进入子 scope,父 scope 里设置的临时变量不会自动继承,子 scope 内对同名变量调用 subtract_from_temp_variable 将从 0 开始计算,导致结果错误。

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

subtract_from_temp_variable is commonly used in scenarios where subtraction operations on temporary variables must occur within conditional blocks—such as calculating resource disparities before comparing against a threshold, or updating accumulators in real-time during complex array iterations. The typical pattern is to first initialize a temporary variable with set_temp_variable, then subtract a fixed value or another variable using this command, and finally validate the result with check_variable.

# Check if a country's total factories minus the target value remains greater than 0
if = {
    limit = {
        set_temp_variable = { var = factory_diff value = num_civilian_factories }
        subtract_from_temp_variable = { var = factory_diff value = 10 }
        check_variable = { factory_diff > 0 }
    }
    # Effect executed when condition is met ...
}

Synergy

  • [set_temp_variable](/wiki/trigger/set_temp_variable): Almost always invoked before this command to initialize the minuend; otherwise the temporary variable remains undefined and the subtraction result becomes unpredictable.
  • [check_variable](/wiki/trigger/check_variable): Applied immediately after subtraction to compare the result—the final step in the "calculate → evaluate" pipeline.
  • [add_to_temp_variable](/wiki/trigger/add_to_temp_variable): Paired with this command to implement mixed addition-subtraction operations, commonly seen in multi-step mathematical derivations (e.g., accumulate first, then deduct penalties).
  • [multiply_temp_variable](/wiki/trigger/multiply_temp_variable): Used immediately after this command in scenarios requiring scaling before subtracting an offset—for example, computing percentage deltas.

Common Pitfalls

  1. Mistakenly using the effect version in a trigger block: subtract_from_temp_variable exists on both trigger and effect whitelists, but their semantics differ—the trigger version only performs calculation and returns a boolean, without persisting any state. If written into an effect block, the game executes the effect version instead, which behaves identically in appearance yet easily causes logical confusion and makes debugging difficult to distinguish between versions.
  2. Temporary variables become invalid across scopes: The lifespan of temp_variable is bound to the current evaluation context. Once you switch into a child scope via any_country, any_state, or similar scope operators, temporary variables set in the parent scope are not automatically inherited. Calling subtract_from_temp_variable on a same-named variable in the child scope will begin calculation from 0, producing incorrect results.