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