Wiki

effect · divide_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Divides a temp variable by a value, another variable, or a [math expression](script_math_expression.md).
`if_zero` specifies the value to assign if the divisor is zero (default is zero).

### Examples

divide_temp_variable = { num_dogs = 2 } divide_temp_variable = { var = num_dogs value = 2 if_zero = 0 } divide_temp_variable = { num_dogs = { value = num_cats add = 1 } }

实战 · 配合 · 坑

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

实战用法

divide_temp_variable 常用于需要计算平均值、比率或按比例缩放数值的 mod 场景,例如将某国总工厂数除以州数来得到"每州平均工厂数",或将某个累计伤亡数除以参战师团数来得到"平均损耗"。以下示例计算人均 IC 并存入临时变量:

# 先将总工业值存入 temp 变量,再除以人口基数
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
}

配合关系

  • [set_temp_variable](/wiki/effect/set_temp_variable):在做除法之前必须先用它初始化被除数,保证变量有一个确定的起始值。
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable):常在除法后紧接着乘以缩放系数(如百分比转换),与 divide_temp_variable 形成"先除后乘"的比例计算链。
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable):除法结果可能超出期望区间,用它将结果限制在合法范围内,防止极端值破坏游戏逻辑。
  • [check_variable](/wiki/trigger/check_variable):计算完比率后,用此触发器检验结果是否满足某个阈值,从而决定后续分支走向。

常见坑

  1. 除数为零未处理:若省略 if_zero 字段,除数为 0 时变量会被置为 0,但在某些版本或特定运算链里这可能静默破坏后续逻辑;务必显式写明 if_zero 的期望回退值,而非依赖默认行为。
  2. 误用 divide_variable 替代divide_temp_variable 操作的是本地临时变量(temp variable),只在当前事件/效果块生命周期内有效;若误用 divide_variable 去除一个本应是临时变量的目标,会意外创建或污染持久变量,造成存档污染或数值异常。

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_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

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