Hands-On Usage
subtract_from_temp_variable is commonly used in scenarios where subtraction operations on temporary variables must occur within conditional blocks—such as calculating resource disparities before comparing against a threshold, or updating accumulators in real-time during complex array iterations. The typical pattern is to first initialize a temporary variable with set_temp_variable, then subtract a fixed value or another variable using this command, and finally validate the result with check_variable.
# Check if a country's total factories minus the target value remains greater than 0
if = {
limit = {
set_temp_variable = { var = factory_diff value = num_civilian_factories }
subtract_from_temp_variable = { var = factory_diff value = 10 }
check_variable = { factory_diff > 0 }
}
# Effect executed when condition is met ...
}
Synergy
[set_temp_variable](/wiki/trigger/set_temp_variable): Almost always invoked before this command to initialize the minuend; otherwise the temporary variable remains undefined and the subtraction result becomes unpredictable.
[check_variable](/wiki/trigger/check_variable): Applied immediately after subtraction to compare the result—the final step in the "calculate → evaluate" pipeline.
[add_to_temp_variable](/wiki/trigger/add_to_temp_variable): Paired with this command to implement mixed addition-subtraction operations, commonly seen in multi-step mathematical derivations (e.g., accumulate first, then deduct penalties).
[multiply_temp_variable](/wiki/trigger/multiply_temp_variable): Used immediately after this command in scenarios requiring scaling before subtracting an offset—for example, computing percentage deltas.
Common Pitfalls
- Mistakenly using the effect version in a trigger block:
subtract_from_temp_variable exists on both trigger and effect whitelists, but their semantics differ—the trigger version only performs calculation and returns a boolean, without persisting any state. If written into an effect block, the game executes the effect version instead, which behaves identically in appearance yet easily causes logical confusion and makes debugging difficult to distinguish between versions.
- Temporary variables become invalid across scopes: The lifespan of
temp_variable is bound to the current evaluation context. Once you switch into a child scope via any_country, any_state, or similar scope operators, temporary variables set in the parent scope are not automatically inherited. Calling subtract_from_temp_variable on a same-named variable in the child scope will begin calculation from 0, producing incorrect results.