命令百科

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 块中。