Wiki

trigger · divide_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

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

### Examples

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

实战 · 配合 · 坑

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

实战用法

divide_temp_variable 常见于需要计算平均值、比率或百分比的复杂数值逻辑中,例如计算某国人均工厂数、战损比或资源分配比例。它在 trigger 条件块内对临时变量执行除法,通常配合其他数学 trigger 完成多步运算后再用 check_variable 做最终判断。

# 判断某国工厂数量是否达到所有省份平均1.5个以上
set_temp_variable = { var = factory_ratio value = num_factories_mio }
divide_temp_variable = { var = factory_ratio value = num_owned_states }
check_variable = { factory_ratio > 1.5 }

配合关系

  • [set_temp_variable](/wiki/trigger/set_temp_variable):在除法之前必须先对临时变量赋初值,否则 divide_temp_variable 操作的变量未定义,结果不可预测。
  • [multiply_temp_variable](/wiki/trigger/multiply_temp_variable):常在除法前后配合使用,例如先乘以 100 再除以基数,将小数结果转为整数百分比便于后续比较。
  • [check_variable](/wiki/trigger/check_variable):除法运算本身不产生真/假,最终必须用 check_variable 对计算结果做阈值判断,才能在条件块里发挥实际作用。
  • [clamp_temp_variable](/wiki/trigger/clamp_temp_variable):除法可能产生超出预期范围的极端值(尤其除数很小时),用 clamp_temp_variable 把结果限制在安全区间内,避免后续逻辑异常。

常见坑

  1. 除以零导致脚本报错或返回异常结果:当除数变量(value 所引用的变量)在运行时可能为 0 时(例如某国刚建立、拥有州数量为 0),必须在执行 divide_temp_variable 前用 check_variableif 先判断除数不为零,否则会造成脚本错误甚至游戏崩溃。
  2. 误将其当作 effect 使用divide_temp_variable 既存在于 trigger 白名单也存在于 effect 白名单,新手容易混淆——在 trigger/limit 块内它只修改临时变量以辅助条件判断,不会持久改变任何游戏状态,因此在 trigger 块内用它做"顺带赋值"来影响后续 effect 是无效的,正式的数值修改必须放在 effect 块中。

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 complex numerical logic that requires calculating averages, ratios, or percentages—such as computing per-capita factory counts, casualty ratios, or resource distribution proportions. It performs division on temporary variables within trigger conditional blocks, typically combined with other mathematical triggers to complete multi-step calculations before making a final judgment using check_variable.

# Check whether a country's factory count reaches an average of 1.5+ per owned state
set_temp_variable = { var = factory_ratio value = num_factories_mio }
divide_temp_variable = { var = factory_ratio value = num_owned_states }
check_variable = { factory_ratio > 1.5 }

Synergy

  • [set_temp_variable](/wiki/trigger/set_temp_variable): The temporary variable must be initialized with a value before division; otherwise the variable referenced by divide_temp_variable is undefined and results are unpredictable.
  • [multiply_temp_variable](/wiki/trigger/multiply_temp_variable): Often used in conjunction before or after division—for example, multiplying by 100 before dividing by a base value to convert fractional results into integer percentages for easier comparison.
  • [check_variable](/wiki/trigger/check_variable): Division itself does not produce a true/false result; you must use check_variable to apply threshold judgment to the calculation result for it to have practical effect in a conditional block.
  • [clamp_temp_variable](/wiki/trigger/clamp_temp_variable): Division may produce extreme values outside the expected range (especially when the divisor is very small); use clamp_temp_variable to constrain the result within a safe interval and prevent subsequent logic errors.

Common Pitfalls

  1. Division by zero causes script errors or abnormal results: When the divisor variable (referenced by the value parameter) could be 0 at runtime—such as a newly founded country with zero owned states—you must check that the divisor is non-zero using check_variable or if before executing divide_temp_variable. Failure to do so will cause script errors or even game crashes.
  2. Mistakenly using it as an effect: divide_temp_variable exists in both the trigger and effect whitelists, which often confuses newcomers. Within trigger/limit blocks, it only modifies the temporary variable to assist condition evaluation and does not persistently alter any game state. Attempting to use it within a trigger block as a "side-effect assignment" to influence subsequent effects is ineffective; actual value modifications must be performed in effect blocks.