Wiki

effect · 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 temp 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 常用于需要在单次事件或 focus 执行块内做临时数值计算的场景,例如计算两个数值的差值、累减循环计数器,或在不污染持久变量的前提下推导中间结果。由于 temp variable 在脚本作用域结束后自动销毁,这个命令特别适合"用完即弃"的逻辑链。

# 示例:计算某国剩余可用建设点(总建设点减去已用点)
set_temp_variable = { var = available_slots value = total_construction_slots }
subtract_from_temp_variable = {
    var   = available_slots
    value = used_construction_slots
}
if = {
    limit = { check_variable = { available_slots > 0 } }
    add_to_variable = { var = bonus_civilian_factories value = available_slots }
}

配合关系

  • [set_temp_variable](/wiki/effect/set_temp_variable) — 通常先用它初始化临时变量的初始值,再用 subtract_from_temp_variable 对其进行减法操作,构成"赋值→运算"的标准流程。
  • [check_variable](/wiki/trigger/check_variable) — 减法完成后,立即用此 trigger 检验结果是否满足阈值条件,决定后续分支走向。
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable) — 减法可能产生负数,配合 clamp 将结果限定在合法范围内,避免下游逻辑出现意外值。
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable) — 在复合公式中,减法结果往往还需要乘以某个系数,两者串联实现更复杂的数值推导。

常见坑

  1. 直接对未初始化的 temp variable 做减法:若目标 var 从未被 set_temp_variableadd_to_temp_variable 赋过值,其初始值为 0,减法会得到负数而不是报错,导致后续逻辑静默出错,难以排查。
  2. 混淆 temp variable 与普通 variable 的作用域:temp variable 仅在当前作用域链内有效,若在 hidden_effect 或嵌套 scope 中设置后又试图在外层读取,值已不存在;此时应改用 subtract_from_variable 操作持久变量。

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 temporary numerical calculations need to be performed within a single event or focus execution block, such as computing the difference between two values, decrementing loop counters, or deriving intermediate results without polluting persistent variables. Since temp variables are automatically destroyed after the script scope ends, this command is particularly suited for "use-and-discard" logic chains.

# Example: Calculate a nation's remaining available construction slots (total slots minus used slots)
set_temp_variable = { var = available_slots value = total_construction_slots }
subtract_from_temp_variable = {
    var   = available_slots
    value = used_construction_slots
}
if = {
    limit = { check_variable = { available_slots > 0 } }
    add_to_variable = { var = bonus_civilian_factories value = available_slots }
}

Synergy

  • [set_temp_variable](/wiki/effect/set_temp_variable) — Typically used first to initialize the initial value of a temp variable, then subtract_from_temp_variable performs subtraction, forming the standard "assignment → operation" workflow.
  • [check_variable](/wiki/trigger/check_variable) — After subtraction is complete, use this trigger to verify whether the result meets threshold conditions and determine subsequent branching logic.
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable) — Subtraction may produce negative numbers; pair it with clamp to confine the result within a valid range, preventing unexpected values in downstream logic.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable) — In compound formulas, subtraction results often need to be multiplied by a coefficient; chain them together to implement more complex numerical derivations.

Common Pitfalls

  1. Performing subtraction directly on an uninitialized temp variable: If the target var has never been assigned a value by set_temp_variable or add_to_temp_variable, its initial value is 0, and subtraction will produce a negative number rather than an error, causing subsequent logic to silently fail and becoming difficult to debug.
  2. Confusing the scope of temp variables with ordinary variables: Temp variables are only valid within the current scope chain. If set within a hidden_effect or nested scope and then accessed from an outer scope, the value no longer exists; in such cases, use subtract_from_variable instead to operate on persistent variables.