Wiki

effect · 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 temp 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 常用于需要在单次事件或决议处理流程中累计计算中间值的场景,例如统计满足条件的州数量、叠加多个来源的奖励数值,而不污染持久化变量。典型用法是先用 set_temp_variable 初始化基础值,再多次调用本指令叠加增量,最后将结果用于判断或赋值给正式变量。

# 统计同盟国数量并存入临时变量
set_temp_variable = { var = ally_count value = 0 }
every_country = {
    limit = { is_in_faction_with = ROOT }
    add_to_temp_variable = { var = ally_count value = 1 }
}
# 此后可用 check_variable 消费 ally_count

配合关系

  • [set_temp_variable](/wiki/effect/set_temp_variable) — 在累加之前用于将临时变量初始化为已知基准值,否则初始值未定义行为难以预测。
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable) — 累加完毕后对临时变量做乘法缩放,常见于按比例折算最终数值的公式链。
  • [subtract_from_temp_variable](/wiki/effect/subtract_from_temp_variable) — 与本指令互为逆操作,在同一计算流程中既可增也可减,共同维护一个动态中间结果。
  • [check_variable](/wiki/trigger/check_variable) — 累加结束后用于读取临时变量的值并做条件分支,是消费 add_to_temp_variable 结果的最常见触发器。

常见坑

  1. 未初始化直接累加:若在使用前没有用 set_temp_variable 赋初值,临时变量的起始值并非保证为 0,可能沿用同一帧内其他逻辑残留的值,导致计算结果不可预期。
  2. 误把临时变量当持久变量使用:临时变量仅在当前脚本执行帧内有效,不能跨事件、跨国家 scope 读取;若需要在后续事件中继续使用计算结果,应在本次执行末尾通过 set_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

add_to_temp_variable is commonly used in scenarios where you need to accumulate intermediate values within a single event or decision processing flow, such as counting states that meet certain conditions or stacking bonuses from multiple sources, without polluting persistent variables. The typical workflow is to first initialize a base value with set_temp_variable, then call this command multiple times to add increments, and finally use the result for conditional checks or assign it to a formal variable.

# Count allied nations and store in temporary variable
set_temp_variable = { var = ally_count value = 0 }
every_country = {
    limit = { is_in_faction_with = ROOT }
    add_to_temp_variable = { var = ally_count value = 1 }
}
# Afterward, check_variable can consume ally_count

Synergy

  • [set_temp_variable](/wiki/effect/set_temp_variable) — Used before accumulation to initialize the temporary variable to a known baseline value; otherwise, undefined initial values make prediction difficult.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable) — Apply multiplicative scaling to the temporary variable after accumulation is complete, commonly used in formula chains that proportionally calculate final values.
  • [subtract_from_temp_variable](/wiki/effect/subtract_from_temp_variable) — The inverse operation of this command; within the same calculation flow, you can both add and subtract, jointly maintaining a dynamic intermediate result.
  • [check_variable](/wiki/trigger/check_variable) — Used after accumulation finishes to read the temporary variable's value and perform conditional branching; this is the most common trigger for consuming results from add_to_temp_variable.

Common Pitfalls

  1. Accumulating without initialization: If set_temp_variable is not used to assign an initial value before use, the starting value of the temporary variable is not guaranteed to be 0 and may retain leftover values from other logic in the same frame, leading to unpredictable calculation results.
  2. Mistakenly treating temporary variables as persistent variables: Temporary variables are only valid within the current script execution frame and cannot be read across events or country scopes. If you need to continue using the calculation result in subsequent events, you should persist it by using set_variable at the end of this execution.