Wiki

trigger · add_to_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Adds a value, a variable, or a [math expression](script_math_expression.md) to a temporary variable.

### Examples

add_to_temp_variable = { num_dogs = 42 } add_to_temp_variable = { num_dogs = { value = num_cats multiply = 2 } }

实战 · 配合 · 坑

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

实战用法

add_to_temp_variable 常见于需要在触发器判断链中做临时数值累加的场景,例如遍历数组统计满足条件的元素数量,或在复杂 check_variable 前先将多个变量值合并到一个临时变量进行比较。典型用途是配合 for_each_scope_loopany_of 逻辑统计动态数值,避免污染持久变量。

# 在 trigger 块中累加后判断
set_temp_variable = { var = temp_count value = 0 }
add_to_temp_variable = { var = temp_count value = var:some_variable }
add_to_temp_variable = { var = temp_count value = 10 }
check_variable = { temp_count > 50 }

配合关系

  • [set_temp_variable](/wiki/trigger/set_temp_variable):几乎总是先用它将临时变量初始化为 0 或某基准值,再用 add_to_temp_variable 叠加,避免读到上一次运算的残留值。
  • [check_variable](/wiki/trigger/check_variable):累加完成后用它对临时变量做阈值判断,是整个"累加→判断"流程的终点。
  • [subtract_from_temp_variable](/wiki/trigger/subtract_from_temp_variable):与加法对称,需要做差值比较时配合使用,共同构成临时数值的加减运算链。
  • [multiply_temp_variable](/wiki/trigger/multiply_temp_variable):当需要对累加结果再做比例缩放(如百分比权重)时搭配使用,完成更复杂的数学表达式。

常见坑

  1. 忘记初始化:直接调用 add_to_temp_variable 而没有事先用 set_temp_variable 清零,临时变量会携带同一帧内前一段逻辑留下的值,导致判断结果不可预测。
  2. 误以为跨 scope 可见:临时变量(temp_)的生命周期仅限于当前事件/效果调用栈,在子 scope(如 any_state 内部)写入的临时变量不会自动传回父 scope,需要在同一层级完成读写,否则累加结果丢失。

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

add_to_temp_variable is commonly used in scenarios where you need to accumulate temporary numerical values within a trigger evaluation chain, such as iterating through arrays to count elements meeting certain conditions, or combining multiple variable values into a single temporary variable before a complex check_variable operation. Typical use cases involve pairing it with for_each_scope_loop or any_of logic to compute dynamic values while avoiding pollution of persistent variables.

# Accumulate within a trigger block and then evaluate
set_temp_variable = { var = temp_count value = 0 }
add_to_temp_variable = { var = temp_count value = var:some_variable }
add_to_temp_variable = { var = temp_count value = 10 }
check_variable = { temp_count > 50 }

Synergy

  • [set_temp_variable](/wiki/trigger/set_temp_variable): Almost always initialize the temporary variable to 0 or a baseline value first, then use add_to_temp_variable to accumulate, avoiding stale values from previous computations in the same frame.
  • [check_variable](/wiki/trigger/check_variable): After accumulation is complete, use it to perform threshold checks on the temporary variable—this is the endpoint of the entire "accumulate → evaluate" pipeline.
  • [subtract_from_temp_variable](/wiki/trigger/subtract_from_temp_variable): Operates symmetrically with addition; use it when you need to perform subtraction comparisons, together forming an arithmetic chain of additions and subtractions on temporary values.
  • [multiply_temp_variable](/wiki/trigger/multiply_temp_variable): Pair with this when you need to apply proportional scaling to the accumulated result (such as percentage weights), enabling more complex mathematical expressions.

Common Pitfalls

  1. Forgetting to initialize: Calling add_to_temp_variable without first zeroing out with set_temp_variable causes the temporary variable to retain values left over from previous logic in the same frame, leading to unpredictable judgment results.
  2. Mistaking it for cross-scope visibility: The lifecycle of temporary variables (temp_) is limited to the current event/effect call stack. Values written to temporary variables in child scopes (such as inside any_state) are not automatically propagated back to the parent scope; you must complete all reads and writes at the same nesting level, otherwise accumulated results are lost.