Wiki

trigger · set_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Sets a temporary variable to a value, another variable, or a [math expression](script_math_expression.md).

### Examples

set_temp_variable = { num_dogs = 42 } set_temp_variable = { var = num_dogs value = 42 tooltip = loc_str_id_with_LEFT_and_RIGHT } set_temp_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_temp_variable is commonly used in scenarios requiring temporary numeric calculations within a single effect/trigger block, such as dynamically computing a country's division count threshold or branch reward multipliers, without polluting persistent variable storage. A typical pattern is to store the intermediate result of a complex expression into a temp variable before an if check, then use check_variable for comparison.

# Calculate target penalty coefficient, valid only within this event logic
set_temp_variable = { var = penalty_threshold value = 10 }
multiply_temp_variable = { var = penalty_threshold value = difficulty_modifier }
if = {
    limit = { check_variable = { penalty_threshold > 15 } }
    add_political_power = -50
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): After set_temp_variable writes an intermediate value, check_variable is nearly always used to perform magnitude or equality comparisons on that temp variable. These two form the core "store→check" pairing.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable): Immediately apply multiplication transform after assigning an initial value to the temp variable, commonly used to scale a baseline value proportionally.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): Combine with set_temp_variable for cumulative operations—first set to initialize or zero, then call add_to_temp_variable multiple times to stack individual contributions.
  • [if](/wiki/trigger/if): Temp variables are meaningless on their own; they only become useful when evaluated through conditional checks in the limit of an if block. set_temp_variable typically appears immediately before the if block.

Common Pitfalls

  1. Temp variables lose scope across blocks: temp_variable is only valid within the current effect/trigger execution stack. It cannot be read in a separate event option or on_action, and its previous value becomes inaccessible. Newcomers often mistakenly treat it as equivalent to a regular variable and use it across event logic, resulting in reads of 0 or errors.
  2. Misusing write operation semantics in trigger blocks: set_temp_variable appears in trigger lists, but semantically it is a "trigger with side effects" utility. Omitting the value field or naming the variable identically to an existing persistent variable will cause subtle, hard-to-trace logic errors. Always keep temp variable names distinct (e.g., prefix with tmp_) to clearly differentiate them.