Wiki

trigger · set_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

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

### Examples

set_temp_variable = { num_dogs = 42 } set_temp_variable = { var = num_dogs value = 42 tooltip = loc_str_id_with_LEFT_and_RIGHT } set_temp_variable = { num_dogs = { value = num_cats multiply = 2 add = 1 } }

实战 · 配合 · 坑

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

实战用法

set_temp_variable 常用于需要在同一个 effect/trigger 块内进行临时数值计算的场景,例如动态计算某国的师团数量阈值、分支奖励倍率等,避免污染持久化的 variable。典型场景是在 if 判断前先把某个复杂表达式的中间结果存入临时变量,再用 check_variable 进行比较。

# 计算目标惩罚系数,仅在本次事件逻辑中有效
set_temp_variable = { var = penalty_threshold value = 10 }
multiply_temp_variable = { var = penalty_threshold value = difficulty_modifier }
if = {
    limit = { check_variable = { penalty_threshold > 15 } }
    add_political_power = -50
}

配合关系

  • [check_variable](/wiki/trigger/check_variable)set_temp_variable 将中间值写入后,几乎必然用 check_variable 来对该临时变量做大小/相等判断,两者构成"存→判"的核心搭档。
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable):对临时变量赋初值后立即进行乘法变换,常用于按比例缩放某个基准数值。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable):与 set_temp_variable 配合做累加运算,先 set 清零/初始化,再多次 add_to_temp_variable 叠加各项贡献值。
  • [if](/wiki/trigger/if):临时变量本身无意义,必须在 iflimit 中通过条件判断才能发挥作用,set_temp_variable 通常紧贴 if 块出现。

常见坑

  1. 临时变量跨块失效temp_variable 仅在当前 effect/trigger 的执行栈内有效,在另一个独立的事件选项或 on_action 中无法读取上一次设置的值,新手常误以为它等同于普通 variable 而在跨事件逻辑中使用,导致读到 0 或报错。
  2. 在 trigger 块中误用写操作语义set_temp_variable 出现在 trigger 列表中,但它本质上是"带副作用的工具型 trigger",如果漏写 value 字段或将变量名与已有持久变量同名,会产生难以追踪的逻辑错误;务必保持临时变量名称独特(如加 tmp_ 前缀)以示区分。

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

set_temp_variable is commonly used in scenarios requiring temporary numeric calculations within a single effect/trigger block, such as dynamically computing a country's division count threshold or branch reward multipliers, without polluting persistent variable storage. A typical pattern is to store the intermediate result of a complex expression into a temp variable before an if check, then use check_variable for comparison.

# Calculate target penalty coefficient, valid only within this event logic
set_temp_variable = { var = penalty_threshold value = 10 }
multiply_temp_variable = { var = penalty_threshold value = difficulty_modifier }
if = {
    limit = { check_variable = { penalty_threshold > 15 } }
    add_political_power = -50
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): After set_temp_variable writes an intermediate value, check_variable is nearly always used to perform magnitude or equality comparisons on that temp variable. These two form the core "store→check" pairing.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable): Immediately apply multiplication transform after assigning an initial value to the temp variable, commonly used to scale a baseline value proportionally.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): Combine with set_temp_variable for cumulative operations—first set to initialize or zero, then call add_to_temp_variable multiple times to stack individual contributions.
  • [if](/wiki/trigger/if): Temp variables are meaningless on their own; they only become useful when evaluated through conditional checks in the limit of an if block. set_temp_variable typically appears immediately before the if block.

Common Pitfalls

  1. Temp variables lose scope across blocks: temp_variable is only valid within the current effect/trigger execution stack. It cannot be read in a separate event option or on_action, and its previous value becomes inaccessible. Newcomers often mistakenly treat it as equivalent to a regular variable and use it across event logic, resulting in reads of 0 or errors.
  2. Misusing write operation semantics in trigger blocks: set_temp_variable appears in trigger lists, but semantically it is a "trigger with side effects" utility. Omitting the value field or naming the variable identically to an existing persistent variable will cause subtle, hard-to-trace logic errors. Always keep temp variable names distinct (e.g., prefix with tmp_) to clearly differentiate them.