命令百科

effect · for_each_scope_loop

Definition

  • Supported scope:any
  • Supported target:any

Description

Runs a loop on for each element of an array and changes scope to current element in each iteration
Example: for_each_scope_loop = {
	array = array_name
	break = break_name #optional (default 'break') set this temp variable to non zero to break the loop
	tooltip = loc #if defined, the effect will output a tooltip for sub effects using this localization as title
 #effect 1
 #effect 2 ...
}

实战 · 配合 · 坑

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

实战用法

for_each_scope_loop 最常用于遍历动态数组并对其中每个元素(国家、州等 scope 对象)执行批量操作,例如给一组目标国家同时施加惩罚、发放物资或触发事件。与 for_each_loop(遍历数值下标)不同,它直接将 scope 切换到数组元素本身,写法更简洁。

# 对存储在 target_countries 数组中的每个国家扣除政治权力
for_each_scope_loop = {
    array = target_countries
    add_political_power = -50
    if = {
        limit = { has_global_flag = war_penalty_active }
        add_stability = -0.05
    }
}

配合关系

  • [add_to_array](/wiki/effect/add_to_array) / [add_to_temp_array](/wiki/effect/add_to_temp_array):循环前先将目标填入数组,是 for_each_scope_loop 的标准前置步骤。
  • [if](/wiki/effect/if):在循环体内用条件判断过滤不满足条件的元素,避免对所有 scope 无差别执行。
  • [set_temp_variable](/wiki/effect/set_temp_variable):在每轮迭代中记录或累加中间计算结果,方便循环结束后统一使用。
  • [find_highest_in_array](/wiki/effect/find_highest_in_array):与循环配合,可先找到数组中的极值再决定循环内的操作逻辑。

常见坑

  1. 数组存放的不是 scope 对象而是纯数值for_each_scope_loop 要求数组元素必须是可以切换 scope 的对象(如国家 TAG、州 ID),若数组里存的是普通数字变量,游戏会报错或静默失败,此时应改用 for_each_loop 搭配下标访问。
  2. 忘记 break 变量的作用域break 字段指定的是一个临时变量名,若在循环外已有同名变量且不为零,循环会在第一次迭代时立即中断;建议在循环开始前用 set_temp_variable = { var = break_name value = 0 } 显式清零。