Wiki

effect · divide_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Divides a variable by a value, another variable, or a [math expression](script_math_expression.md).

### Examples

divide_variable = { num_dogs = 2 } divide_variable = { num_dogs = { value = num_cats add = 1 } }

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

divide_variable 常用于需要对变量做归一化或平均值计算的 mod 场景,例如将某国积累的"总资源收益"除以回合数以得到"每回合平均值",或将一个人口变量按比例缩放以适配 UI 显示范围。以下是一个计算平均工厂产出的示例:

# 将累计产出总量除以统计周期数,得到平均产出
divide_variable = {
    var = total_factory_output
    value = num_of_weeks
}

配合关系

  • [set_variable](/wiki/effect/set_variable):在执行除法之前,通常需要先用 set_variable 给目标变量赋初始值,确保被除数有意义的起点。
  • [add_to_variable](/wiki/effect/add_to_variable):常在循环或事件中持续累加变量,最后再用 divide_variable 求平均,两者构成"累加→平均"工作流。
  • [clamp_variable](/wiki/effect/clamp_variable):除法结果可能出现极端值(如除数很大时趋近于零),用 clamp_variable 在除法后将结果限定在合理区间内。
  • [check_variable](/wiki/trigger/check_variable):除法执行前应先用 check_variable 触发器确认除数不为零,避免脚本产生非预期行为。

常见坑

  1. 除数为零未做保护:HOI4 脚本不会自动报除零错误,但结果会变成 0 或产生异常值。务必在 divide_variable 前使用 [check_variable](/wiki/trigger/check_variable)(配合 if 块)判断除数变量的值是否大于零,再执行除法。
  2. 混淆 var 字段与 value 字段的类型value 可以填写固定数字,也可以填写另一个变量名,但若填写的变量名在当前 scope 下不存在,游戏会静默将其视为 0,导致结果异常,新手容易误以为是逻辑错误而长时间排查。

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

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

  1. 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.
  2. 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.