Wiki

effect · set_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Sets a temp variable to a value, another variable, or a [math expression](script_math_expression.md).
`tooltip` can be used to override tooltip title with LEFT and RIGHT tokens.

### 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 块内进行临时数值运算的场景,例如动态计算奖励兵力数量、根据国家当前状态临时存储中间值再传入循环。由于 temp variable 仅在当前 effect 块的生命周期内存在,它天然适合"用完即弃"的计算过程,不会污染全局变量空间。

# 在事件中临时计算要给予的补给量
option = {
    name = my_event.1.a
    set_temp_variable = { var = bonus_supply value = 50 }
    multiply_temp_variable = { var = bonus_supply value = stability }
    add_to_variable = { var = national_supply value = bonus_supply }
}

配合关系

  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable) — 先用 set_temp_variable 设置初始值,再用乘法指令对其缩放,是构建多步数学表达式的标准套路。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable) — 对同一个 temp variable 进行累加,常用于在循环内累积统计结果。
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable) — 运算完成后对 temp variable 进行上下界约束,防止数值溢出或产生非预期的极端值。
  • [check_variable](/wiki/trigger/check_variable) — 在 if 块的条件中读取刚刚设置好的 temp variable,决定后续执行哪条分支。

常见坑

  1. 误以为 temp variable 可以跨事件链传递:temp variable 的作用域仅限于当前执行块,事件结束后即销毁。如果需要在下一个事件或 on_action 中继续使用该值,必须改用 [set_variable](/wiki/effect/set_variable) 将其持久化到普通变量。
  2. 直接用 temp variable 名作为 value 引用时漏写正确引用方式:将另一个变量的值赋给 temp variable 时,value 字段应填写源变量的完整引用路径(如 ROOT.some_var),而不是裸字面量,否则会被解析为数字 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

set_temp_variable is commonly used in scenarios requiring temporary numerical calculations within a single event or effect block, such as dynamically computing troop reinforcement amounts or storing intermediate values based on the current state of a country before passing them into loops. Since temp variables exist only within the lifecycle of the current effect block, they are naturally suited for "use-and-discard" calculation processes without polluting the global variable namespace.

# Temporarily calculate supply amount to be granted in an event
option = {
    name = my_event.1.a
    set_temp_variable = { var = bonus_supply value = 50 }
    multiply_temp_variable = { var = bonus_supply value = stability }
    add_to_variable = { var = national_supply value = bonus_supply }
}

Synergy

  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable) — Set an initial value with set_temp_variable, then scale it using multiplication commands. This is the standard pattern for constructing multi-step mathematical expressions.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable) — Accumulate the same temp variable, commonly used to aggregate statistical results within loops.
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable) — Apply upper and lower bounds to the temp variable after calculations to prevent value overflow or unexpected extreme values.
  • [check_variable](/wiki/trigger/check_variable) — Read the newly set temp variable in the condition of an if block to determine which branch executes next.

Common Pitfalls

  1. Mistakenly assuming temp variables can persist across event chains: The scope of temp variables is limited to the current execution block and they are destroyed after the event ends. If you need to use that value in the next event or on_action, you must use [set_variable](/wiki/effect/set_variable) instead to persist it as a regular variable.
  2. Omitting the correct reference syntax when using a temp variable name as a value reference: When assigning another variable's value to a temp variable, the value field should contain the complete reference path of the source variable (e.g., ROOT.some_var), not a bare literal. Otherwise it will be parsed as the number 0 or cause an error.