Hands-On Usage
for_each_scope_loop is most commonly used to iterate through dynamic arrays and execute batch operations on each element (scope objects such as countries, states, etc.), such as simultaneously applying penalties to a set of target countries, distributing resources, or triggering events. Unlike for_each_loop (which iterates through numeric indices), it directly switches the scope to the array elements themselves, resulting in cleaner syntax.
# Deduct political power from each country stored in the target_countries array
for_each_scope_loop = {
array = target_countries
add_political_power = -50
if = {
limit = { has_global_flag = war_penalty_active }
add_stability = -0.05
}
}
Synergy
[add_to_array](/wiki/effect/add_to_array) / [add_to_temp_array](/wiki/effect/add_to_temp_array): Populate target elements into an array before the loop—this is the standard prerequisite step for for_each_scope_loop.
[if](/wiki/effect/if): Use conditional checks within the loop body to filter elements that do not meet conditions, avoiding indiscriminate execution across all scopes.
[set_temp_variable](/wiki/effect/set_temp_variable): Record or accumulate intermediate calculation results during each iteration for unified use after the loop completes.
[find_highest_in_array](/wiki/effect/find_highest_in_array): Combined with the loop, find extreme values in the array first before deciding the operation logic within the loop.
Common Pitfalls
- Array contains pure numeric values instead of scope objects:
for_each_scope_loop requires array elements to be objects that can have their scope switched (such as country TAGs or state IDs). If the array stores ordinary numeric variables, the game will error or fail silently. In such cases, use for_each_loop with index access instead.
- Forgetting to clear the break variable's scope: The
break field specifies a temporary variable name. If a variable with the same name already exists outside the loop and is non-zero, the loop will terminate immediately on the first iteration. It is recommended to explicitly reset it with set_temp_variable = { var = break_name value = 0 } before the loop begins.