Hands-On Usage
divide_temp_variable is commonly used in mod scenarios that require calculating averages, ratios, or proportionally scaling values—for example, dividing a nation's total factories by the number of states to obtain "average factories per state," or dividing cumulative casualties by the number of participating divisions to derive "average losses per division." The following example calculates per-capita IC and stores it in a temporary variable:
# First store total industrial capacity in a temp variable, then divide by population base
set_temp_variable = { var = per_capita_ic value = ROOT.industrial_capacity_factory }
divide_temp_variable = {
var = per_capita_ic
value = ROOT.manpower
if_zero = 0
}
Synergy
[set_temp_variable](/wiki/effect/set_temp_variable): Must be used to initialize the dividend before performing division, ensuring the variable has a well-defined starting value.
[multiply_temp_variable](/wiki/effect/multiply_temp_variable): Commonly applied immediately after division to multiply by a scaling factor (such as percentage conversion), forming a "divide-then-multiply" proportional calculation chain with divide_temp_variable.
[clamp_temp_variable](/wiki/effect/clamp_temp_variable): Division results may exceed the expected range; use this to constrain the result within valid bounds and prevent extreme values from breaking game logic.
[check_variable](/wiki/trigger/check_variable): After computing the ratio, use this trigger to verify whether the result meets a certain threshold, thereby determining the subsequent branching path.
Common Pitfalls
- Division by zero not handled: If the
if_zero field is omitted, the variable will be set to 0 when the divisor is 0, but in some versions or specific calculation chains this may silently break subsequent logic; always explicitly specify the expected fallback value for if_zero rather than relying on default behavior.
- Mistakenly using
divide_variable as a substitute: divide_temp_variable operates on local temporary variables (temp variables) that are only valid within the current event/effect block lifecycle; if divide_variable is incorrectly used on a target that should be a temporary variable, it will unexpectedly create or pollute persistent variables, resulting in save corruption or numerical anomalies.