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 } }

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.