Hands-On Usage
add_to_temp_array is commonly used during event or decision execution to temporarily collect a batch of countries, values, or variables that meet certain criteria, then process them uniformly through iteration, avoiding pollution of persistent arrays. For example, when calculating the number of belligerents, you can progressively push qualifying countries into a temporary array, then iterate through it using for_each_scope_loop to process them in batch. After the function completes, the array is automatically destroyed without manual cleanup.
# Collect all major countries in a state of war to a temporary array, then process in batch
every_country = {
limit = { is_major = yes has_war = yes }
add_to_temp_array = {
array = warring_majors
value = THIS
}
}
for_each_scope_loop = {
array = warring_majors
add_to_temp_variable = { warring_count = 1 }
}
Synergy
[clear_temp_array](/wiki/effect/clear_temp_array): Clear a temporary array before reusing it under the same name to prevent residual elements from the previous execution from interfering with current logic.
[remove_from_temp_array](/wiki/effect/remove_from_temp_array): After pushing elements, if you discover a particular item no longer meets the criteria, you can dynamically remove it from the array to keep it clean.
[for_each_scope_loop](/wiki/effect/for_each_scope_loop): The most typical consumer of temporary arrays, iterating through each element in the array and bringing it into scope to execute subsequent effects. It forms the canonical "collect → iterate" pairing with add_to_temp_array.
[collection_size](/wiki/trigger/collection_size): Read the number of elements in a temporary array from the trigger side to determine whether the collected objects have reached a threshold before deciding which branch to execute.
Common Pitfalls
- Mistakenly using it for persistent storage: Temporary arrays exist only during the current event or effect block execution and disappear once execution completes. Beginners sometimes expect to read the data when the next event triggers, but the data is already lost. Persistent storage requirements should use
[add_to_array](/wiki/effect/add_to_array) instead.
- Forgetting to initialize outside the loop, causing cumulative duplication: If the same event can trigger multiple times and each time pushes elements to a temporary array with the same name, but
[clear_temp_array](/wiki/effect/clear_temp_array) is not called at the logic entry point, array contents will accumulate, causing bugs from duplicate processing.