Hands-On Usage
subtract_from_temp_variable is commonly used in scenarios where temporary numerical calculations need to be performed within a single event or focus execution block, such as computing the difference between two values, decrementing loop counters, or deriving intermediate results without polluting persistent variables. Since temp variables are automatically destroyed after the script scope ends, this command is particularly suited for "use-and-discard" logic chains.
# Example: Calculate a nation's remaining available construction slots (total slots minus used slots)
set_temp_variable = { var = available_slots value = total_construction_slots }
subtract_from_temp_variable = {
var = available_slots
value = used_construction_slots
}
if = {
limit = { check_variable = { available_slots > 0 } }
add_to_variable = { var = bonus_civilian_factories value = available_slots }
}
Synergy
[set_temp_variable](/wiki/effect/set_temp_variable) — Typically used first to initialize the initial value of a temp variable, then subtract_from_temp_variable performs subtraction, forming the standard "assignment → operation" workflow.
[check_variable](/wiki/trigger/check_variable) — After subtraction is complete, use this trigger to verify whether the result meets threshold conditions and determine subsequent branching logic.
[clamp_temp_variable](/wiki/effect/clamp_temp_variable) — Subtraction may produce negative numbers; pair it with clamp to confine the result within a valid range, preventing unexpected values in downstream logic.
[multiply_temp_variable](/wiki/effect/multiply_temp_variable) — In compound formulas, subtraction results often need to be multiplied by a coefficient; chain them together to implement more complex numerical derivations.
Common Pitfalls
- Performing subtraction directly on an uninitialized temp variable: If the target
var has never been assigned a value by set_temp_variable or add_to_temp_variable, its initial value is 0, and subtraction will produce a negative number rather than an error, causing subsequent logic to silently fail and becoming difficult to debug.
- Confusing the scope of temp variables with ordinary variables: Temp variables are only valid within the current scope chain. If set within a
hidden_effect or nested scope and then accessed from an outer scope, the value no longer exists; in such cases, use subtract_from_variable instead to operate on persistent variables.