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