Wiki

effect · for_loop_effect

Definition

  • Supported scope:any
  • Supported target:any

Description

Runs a same effects through a loop. example will run the effects for value_name = -3, 0, 3, 6, 9 and then terminate
Example: for_loop_effect = {
	start = -3 (default 0) start value of loop
	end = 10 (default 0) end value of loop
	compare = less_than_or_equals (default less_than) comparison type between start and end val
	add = 3 (default 1) value to add to current value after each iteration
	value = value_name #optional (default 'v') current value of iteration will be stored in this temp variable
	break = break_name #optional (default 'break') set this temp variable to non zero to break the loop
 #effect 1
 #effect 2 ...
}

实战 · 配合 · 坑

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

实战用法

for_loop_effect 适合需要批量、规律性重复执行效果的场景,例如按固定间隔为多个省份累加变量、动态填充数组,或批量创建具有递增参数的单位。下面示例展示如何将 0、10、20、30、40 依次累加到一个变量中:

for_loop_effect = {
    start = 0
    end = 50
    compare = less_than
    add = 10
    value = current_step
    add_to_variable = {
        var = my_total
        value = var:current_step
    }
}

配合关系

  • [set_temp_variable](/wiki/effect/set_temp_variable) — 在循环体内初始化或重置中间临时变量,避免上一轮迭代的残留值干扰当前计算。
  • [add_to_array](/wiki/effect/add_to_array) — 常在循环中将每轮的 value(迭代变量)推入数组,便于后续用 for_each_loopfind_highest_in_array 进一步处理。
  • [if](/wiki/effect/if) — 在循环体内做条件判断,根据当前迭代值决定是否执行某段逻辑,或配合 break 变量提前终止循环。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable) — 利用迭代变量做累加运算时的核心搭档,将每轮的增量安全地叠加到临时变量上。

常见坑

  1. 忘记 break 变量的重置break 临时变量一旦被设为非零,循环就会终止;若在同一事件的其他逻辑中该变量已被赋值且未清零,下次进入循环时会立即退出,导致循环体一次都不执行。建议在循环前用 set_temp_variable = { var = break_name value = 0 } 显式清零。
  2. endcompare 的边界混淆:默认比较方式是 less_than(严格小于),若把 end 设成与目标最后一个期望值相同,最后一次迭代实际上不会执行——需要改用 less_than_or_equals 或将 end 加一,否则末尾迭代会静默跳过,产生难以察觉的差一错误。

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

for_loop_effect is well-suited for scenarios requiring batch or repetitive execution of effects at regular intervals, such as incrementally adding variables to multiple provinces, dynamically populating arrays, or batch-creating units with incrementing parameters. The example below demonstrates how to sequentially accumulate 0, 10, 20, 30, 40 into a single variable:

for_loop_effect = {
    start = 0
    end = 50
    compare = less_than
    add = 10
    value = current_step
    add_to_variable = {
        var = my_total
        value = var:current_step
    }
}

Synergy

  • [set_temp_variable](/wiki/effect/set_temp_variable) — Initialize or reset intermediate temporary variables within the loop body to prevent residual values from previous iterations from interfering with current calculations.
  • [add_to_array](/wiki/effect/add_to_array) — Commonly used in loops to push each iteration's value (loop variable) into an array for subsequent processing via for_each_loop or find_highest_in_array.
  • [if](/wiki/effect/if) — Perform conditional checks within the loop body to decide whether to execute certain logic based on the current iteration value, or pair with the break variable to terminate the loop early.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable) — Core companion when performing accumulation operations with iteration variables, safely stacking each round's increment onto the temporary variable.

Common Pitfalls

  1. Forgetting to reset the break variable: Once the break temporary variable is set to a non-zero value, the loop terminates; if the variable has been assigned elsewhere in the same event and not cleared, the loop will exit immediately on the next entry, causing the loop body to never execute. It is recommended to explicitly reset with set_temp_variable = { var = break_name value = 0 } before the loop.
  2. Confusion between end and compare boundaries: The default comparison method is less_than (strictly less than). If you set end to the same value as the final expected iteration target, the last iteration will not actually execute—you must either switch to less_than_or_equals or increment end by one, otherwise the final iteration will silently skip, creating a hard-to-detect off-by-one error.