Hands-On Usage
add_to_array is commonly used in mod scenarios where you need to dynamically track a set of countries, state IDs, or numeric values—for example, recording a list of "occupied core states" or "all country TAGs in an alliance." Combined with loops and array iteration, you can implement complex custom logic, such as counting states that meet certain conditions and making that count available to subsequent effects.
# Collect all core state IDs occupied by enemies into an array for subsequent processing
clear_array = { array = occupied_core_states }
every_state = {
limit = {
is_core_of = ROOT
NOT = { is_owned_by = ROOT }
}
add_to_array = {
array = occupied_core_states
value = THIS
}
}
Synergy
[clear_array](/wiki/effect/clear_array) — Clear old data before repopulating an array to prevent unlimited growth or retention of stale elements.
[remove_from_array](/wiki/effect/remove_from_array) — Use symmetrically with add_to_array to remove elements from the array when they no longer meet conditions, maintaining array accuracy.
[for_each_scope_loop](/wiki/effect/for_each_scope_loop) / [random_scope_in_array](/wiki/effect/random_scope_in_array) — After writing elements to an array, these two commands are commonly used to iterate over or randomly select scope elements from the array to execute subsequent logic.
[is_in_array](/wiki/trigger/is_in_array) — After writing to an array, use this command on the trigger side to check whether a given value already exists in the array; commonly used for deduplication or conditional checks.
Common Pitfalls
- Confusing temporary arrays with persistent arrays: If you only need to use an array temporarily within an event, use
add_to_temp_array instead. Data written with add_to_array persists with the save file; if you forget to call clear_array at the appropriate time, the array will accumulate stale data across events, leading to logic errors.
- Omitting
value writes the current scope, not a numeric value: When the value field is absent, add_to_array defaults to writing the current scope (such as the current country or state) as an element. If you intend to write a numeric variable, you must explicitly specify value = <variable or number>; otherwise, subsequent reads using check_variable or arithmetic operations on that array will fail due to type mismatch.