Wiki

effect · set_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Sets a 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_variable = { num_dogs = 42 } set_variable = { var = num_dogs value = 42 tooltip = loc_str_id_with_LEFT_and_RIGHT } set_variable = { num_dogs = { value = num_cats multiply = 2 add = 1 } }

实战 · 配合 · 坑

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

实战用法

set_variable 常用于追踪动态数值状态,例如记录玩家积累的资源点数、阶段进度或自定义计数器。与其他算术 effect 配合,可构建完整的"变量经济系统",如每次触发事件时先 set_variable 初始化基准值,再叠加修正量。

# 初始化一场内战的伤亡计数器
country_event = {
    id = my_mod.1
    immediate = {
        set_variable = {
            var = civil_war_casualties
            value = 0
        }
    }
    option = {
        name = my_mod.1.a
        # 后续用 add_to_variable 累加
        add_to_variable = {
            var = civil_war_casualties
            value = 500
        }
    }
}

配合关系

  • [add_to_variable](/wiki/effect/add_to_variable)set_variable 负责初始化或重置基准值,add_to_variable 负责后续累加,两者是最经典的"初始化+累积"搭档。
  • [clamp_variable](/wiki/effect/clamp_variable) — 设置完变量后立即用 clamp_variable 限制上下界,防止数值越界导致脚本逻辑异常。
  • [check_variable](/wiki/trigger/check_variable) — 在 trigger 侧读取 set_variable 写入的值,判断是否满足事件或决策的触发条件,形成"写入—读取"闭环。
  • [multiply_variable](/wiki/effect/multiply_variable) — 先用 set_variable 赋一个基础值,再用 multiply_variable 乘以某个系数,实现灵活的比例计算。

常见坑

  1. 作用域混淆set_variable 写入的变量归属于当前 scope(国家、州、角色等),若在国家 scope 下写入后跑到州 scope 里直接读取同名变量,会发现值不存在或读到旧值——必须通过 ROOT/FROM 等限定词明确指向正确 scope。
  2. 覆盖而非累加:新手常把 set_variableadd_to_variable 使用,在循环或连续触发中每次都用 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

set_variable is commonly used to track dynamic numeric states, such as recording accumulated resource points, progression stages, or custom counters. When combined with other arithmetic effects, it can build a complete "variable economy system"—for example, initializing a baseline value with set_variable each time an event triggers, then stacking correction modifiers on top.

# Initialize a casualty counter for a civil war
country_event = {
    id = my_mod.1
    immediate = {
        set_variable = {
            var = civil_war_casualties
            value = 0
        }
    }
    option = {
        name = my_mod.1.a
        # Use add_to_variable for subsequent accumulation
        add_to_variable = {
            var = civil_war_casualties
            value = 500
        }
    }
}

Synergy

  • [add_to_variable](/wiki/effect/add_to_variable)set_variable handles initialization or resetting the baseline, while add_to_variable handles subsequent accumulation; they are the classic "initialize-then-accumulate" duo.
  • [clamp_variable](/wiki/effect/clamp_variable) — Immediately after setting a variable, use clamp_variable to constrain its upper and lower bounds, preventing overflow that could break script logic.
  • [check_variable](/wiki/trigger/check_variable) — Read values written by set_variable on the trigger side to determine whether event or decision conditions are met, forming a complete "write-read" loop.
  • [multiply_variable](/wiki/effect/multiply_variable) — First assign a base value with set_variable, then multiply it by a coefficient using multiply_variable to enable flexible proportional calculations.

Common Pitfalls

  1. Scope Confusion: Variables written by set_variable belong to the current scope (country, state, character, etc.). If you write to a variable in country scope and then try to read the same variable directly in state scope, you'll find the value doesn't exist or contains a stale value—you must use scope limiters like ROOT or FROM to correctly specify the target scope.
  2. Overwriting Instead of Accumulating: Beginners often mistake set_variable for add_to_variable, repeatedly assigning fixed values with set_variable during loops or continuous triggers, completely overwriting and zeroing out previous accumulated results. You must distinguish between "initialization" and "incremental modification" timing.