Wiki

trigger · has_variable

Definition

  • Supported scope:any
  • Supported target:any

Description

Checks if a variable exists in a scope

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

has_variable is commonly used to check whether a custom variable has been written to a scope via set_variable or add_to_variable, preventing script errors or unexpected behavior from directly reading non-existent variables. Typical scenarios include: determining whether the player has completed a prerequisite step in a country event chain (using the variable as a marker), or safely accessing dynamically generated values in effects triggered by on_action.

# Only allow the option if this country has recorded the "reform_progress" variable
available = {
    has_variable = reform_progress
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): has_variable first confirms the variable exists, then check_variable compares its specific value; the two almost always appear in tandem to prevent numeric comparisons on null variables.
  • [set_variable](/wiki/effect/set_variable): After writing a variable on the effect side, use has_variable on the trigger side to verify the write has occurred, forming a complete "write-check" workflow.
  • [if](/wiki/trigger/if): Use has_variable as the limit condition in an if statement to implement branch protection with "execute subsequent logic only if variable exists."
  • [clear_variable](/wiki/effect/clear_variable): After clearing a variable, has_variable returns false; this behavior can be leveraged to make "reset state" determinations without needing to set additional boolean flag variables.

Common Pitfalls

  1. Using check_variable directly instead of this trigger: Beginners often skip the prerequisite check of has_variable and perform numeric comparisons directly on variables that may not exist. If a variable has not yet been assigned, check_variable behavior may not match expectations (typically treated as 0), easily producing subtle logic errors in complex event chains.
  2. Variable name spelling inconsistencies: The parameter for has_variable is a string-form variable name that must match exactly with the name used during assignment (including capitalization). Cross-file references with inconsistent naming causing checks to always return false is a common debugging pitfall.