Wiki

effect · modulo_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

modulos a temp variable with another. Example: 
modulo_temp_variable = { 
  var = variable_to_modulo 
  value = divisior 
}

实战 · 配合 · 坑

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

实战用法

modulo_temp_variable 常用于循环计数、周期性判断或将数值限定在某个范围内的场景,例如每隔 N 天触发一次事件、或将随机值映射到固定区间。在生成程序化内容(如地图格子编号分组、轮询国家列表索引)时也非常实用。

# 示例:判断当前循环计数是否是3的倍数
set_temp_variable = { var = remainder value = counter }
modulo_temp_variable = { var = remainder value = 3 }
if = {
    limit = { check_variable = { var = remainder value = 0 } }
    # 每3次执行一次的逻辑
    add_to_variable = { var = milestone_count value = 1 }
}

配合关系

  • [set_temp_variable](/wiki/effect/set_temp_variable) — 在取模运算前先将目标变量赋值或拷贝,避免直接修改原始变量。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable) — 与计数器配合,每次循环累加后再取模,实现周期性触发逻辑。
  • [check_variable](/wiki/trigger/check_variable) — 取模结果通常立即用此 trigger 判断余数是否为 0(或某一预期值),决定后续分支。
  • [for_loop_effect](/wiki/effect/for_loop_effect) — 循环体内常配合取模来做分组处理,例如将循环索引按组数取模以分配不同行为。

常见坑

  1. 直接对原始变量取模导致数据丢失modulo_temp_variable 会就地修改 var 指定的变量,如果直接写入业务用的计数器变量,该变量的原始值就被余数覆盖了。应先用 set_temp_variable 把值拷贝到一个临时中间变量,再对中间变量做取模。
  2. 除数(value)为 0 导致脚本异常value 字段若在运行时解析为 0,会引发除零错误使效果静默失败甚至报错。在 value 来自动态变量时,务必先用 [check_variable](/wiki/trigger/check_variable)[clamp_temp_variable](/wiki/effect/clamp_temp_variable) 保证其大于 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

modulo_temp_variable is commonly used for loop counting, periodic checks, or constraining values within a specific range—for example, triggering an event every N days, or mapping random values to fixed intervals. It's also highly practical for procedurally generating content such as grouping map tile indices or cycling through country list indices.

# Example: Check if the current loop counter is a multiple of 3
set_temp_variable = { var = remainder value = counter }
modulo_temp_variable = { var = remainder value = 3 }
if = {
    limit = { check_variable = { var = remainder value = 0 } }
    # Logic that executes once every 3 iterations
    add_to_variable = { var = milestone_count value = 1 }
}

Synergy

  • [set_temp_variable](/wiki/effect/set_temp_variable) — Assign or copy the target variable before the modulo operation to avoid modifying the original variable directly.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable) — Pair with a counter; increment it each loop iteration and then apply modulo to implement periodic trigger logic.
  • [check_variable](/wiki/trigger/check_variable) — The modulo result is typically checked immediately with this trigger to determine whether the remainder equals 0 (or an expected value) and branch accordingly.
  • [for_loop_effect](/wiki/effect/for_loop_effect) — Often combined with modulo inside the loop body for grouping operations—for example, applying modulo to the loop index by group count to assign different behaviors.

Common Pitfalls

  1. Applying modulo directly to the original variable causes data loss: modulo_temp_variable modifies the variable specified by var in place. If you apply it directly to a business-critical counter variable, the original value is overwritten by the remainder. Instead, first use set_temp_variable to copy the value to a temporary intermediate variable, then apply modulo to that intermediate variable.
  2. Division by zero (value = 0) causes script failure: If the value field resolves to 0 at runtime, it triggers a division-by-zero error that silently fails the effect or throws an error. When value comes from a dynamic variable, always use [check_variable](/wiki/trigger/check_variable) or [clamp_temp_variable](/wiki/effect/clamp_temp_variable) to ensure it is greater than 0.