Hands-On Usage
add_to_temp_variable is commonly used in scenarios where you need to accumulate intermediate values within a single event or decision processing flow, such as counting states that meet certain conditions or stacking bonuses from multiple sources, without polluting persistent variables. The typical workflow is to first initialize a base value with set_temp_variable, then call this command multiple times to add increments, and finally use the result for conditional checks or assign it to a formal variable.
# Count allied nations and store in temporary variable
set_temp_variable = { var = ally_count value = 0 }
every_country = {
limit = { is_in_faction_with = ROOT }
add_to_temp_variable = { var = ally_count value = 1 }
}
# Afterward, check_variable can consume ally_count
Synergy
[set_temp_variable](/wiki/effect/set_temp_variable) — Used before accumulation to initialize the temporary variable to a known baseline value; otherwise, undefined initial values make prediction difficult.
[multiply_temp_variable](/wiki/effect/multiply_temp_variable) — Apply multiplicative scaling to the temporary variable after accumulation is complete, commonly used in formula chains that proportionally calculate final values.
[subtract_from_temp_variable](/wiki/effect/subtract_from_temp_variable) — The inverse operation of this command; within the same calculation flow, you can both add and subtract, jointly maintaining a dynamic intermediate result.
[check_variable](/wiki/trigger/check_variable) — Used after accumulation finishes to read the temporary variable's value and perform conditional branching; this is the most common trigger for consuming results from add_to_temp_variable.
Common Pitfalls
- Accumulating without initialization: If
set_temp_variable is not used to assign an initial value before use, the starting value of the temporary variable is not guaranteed to be 0 and may retain leftover values from other logic in the same frame, leading to unpredictable calculation results.
- Mistakenly treating temporary variables as persistent variables: Temporary variables are only valid within the current script execution frame and cannot be read across events or country scopes. If you need to continue using the calculation result in subsequent events, you should persist it by using
set_variable at the end of this execution.