Wiki

effect · clear_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Clears a variable
Example: clear_variable = num_dogs

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

clear_variable is commonly used in mods for variable lifecycle management. For example, after an event chain concludes, you can clear counters or temporary state flags that are no longer needed, preventing variable residue from causing logic errors in subsequent checks. It can also reset iteration variables at the end of loops or recursive effects, keeping your script state clean.

# When a diplomatic negotiation event chain ends, clear the variable tracking round counts
country_event = {
    id = diplomacy.10
    option = {
        name = diplomacy.10.a
        # Handle settlement logic...
        clear_variable = negotiation_rounds
    }
}

Synergy

  • [set_variable](/wiki/effect/set_variable): set_variable initializes or assigns a variable, while clear_variable is its corresponding "destruction" operation. Together they form the complete create-destroy lifecycle of a variable.
  • [has_variable](/wiki/trigger/has_variable): Before calling clear_variable, it's recommended to first verify the variable exists using the has_variable trigger to avoid unexpected behavior when clearing a non-existent variable.
  • [if](/wiki/effect/if): It's standard practice to wrap clear_variable within an if conditional block paired with has_variable for defensive checks—a hallmark of defensive script writing.
  • [clear_array](/wiki/effect/clear_array): If the same logic uses both variables and arrays to track state, clear_variable and clear_array often appear together to clean up all temporary data at once.

Common Pitfalls

  1. Reading immediately after clearing: Beginners sometimes call clear_variable first within the same option block, then reference that variable later (such as with check_variable). Since the variable no longer exists, the condition will never evaluate true or the script will error out. Always ensure variables are cleared only after all read operations are complete.
  2. Incorrectly writing scope syntax: The value for clear_variable is simply the variable name as a string (e.g., clear_variable = num_dogs). It does not support syntax like var:num_dogs or scope-prefixed forms. Copying the assignment syntax from set_variable will cause parsing to fail.