Hands-On Usage
divide_variable is commonly used in mod scenarios requiring variable normalization or average value calculations, such as dividing a country's accumulated "total resource income" by the number of turns to obtain "average per turn," or scaling a population variable proportionally to fit UI display ranges. Below is an example of calculating average factory output:
# Divide accumulated output total by the statistics period count to get average output
divide_variable = {
var = total_factory_output
value = num_of_weeks
}
Synergy
[set_variable](/wiki/effect/set_variable): Before performing division, you typically need to use set_variable first to assign an initial value to the target variable, ensuring the dividend has a meaningful starting point.
[add_to_variable](/wiki/effect/add_to_variable): Commonly used to continuously accumulate variables in loops or events, then apply divide_variable to calculate the average; together they form an "accumulate → average" workflow.
[clamp_variable](/wiki/effect/clamp_variable): Division results may produce extreme values (such as approaching zero when the divisor is very large); use clamp_variable after division to constrain the result within a reasonable range.
[check_variable](/wiki/trigger/check_variable): Before executing division, use the check_variable trigger to confirm the divisor is not zero, avoiding unexpected script behavior.
Common Pitfalls
- No protection against zero divisor: HOI4 scripts will not automatically report a division-by-zero error, but the result becomes
0 or produces anomalous values. Always use [check_variable](/wiki/trigger/check_variable) (combined with an if block) before divide_variable to check whether the divisor variable is greater than zero before performing the division.
- Confusing the type of
var and value fields: value can accept a fixed number or another variable name, but if the variable name filled in does not exist in the current scope, the game silently treats it as 0, resulting in anomalous output; beginners often mistake this for a logic error and spend considerable time debugging.