Hands-On Usage
random_scope_in_array is commonly used to pick a single random target from a dynamically built list of countries or states and perform an operation on it — for example, randomly selecting one ally to receive aid during an event, or picking a random province from a stored list to apply a sabotage effect. Unlike for_each_scope_loop, it executes only once, avoiding the performance overhead of bulk triggering.
# Pick a random country from a pre-built array and transfer a state to it
random_scope_in_array = {
array = candidate_countries
limit = {
is_in_array = { array = candidate_countries value = THIS }
has_variable = ready_flag
}
save_event_target_as = chosen_target
}
# Afterwards, use event_target:chosen_target scope for further operations such as goto_state
Synergy
- add_to_array / add_to_temp_array: Push qualifying targets into an array one by one, then use
random_scope_in_array to draw from it — this is the classic "collect then randomize" pattern.
- save_event_target_as: Save the randomly selected scope as an event target inside the block, so the same target can be reused in subsequent logic outside the block.
- set_temp_variable / set_variable: Record a value from the selected target inside the block, or combine with the
break mechanism to stop early once a satisfactory target is found — effectively implementing "stop at the first match" logic.
- is_in_array: Use inside a
limit block to cross-check whether the current scope still exists in a secondary helper array, filtering out elements that have already been processed at runtime.
Common Pitfalls
- Forgetting whether the array holds scopes or plain values:
random_scope_in_array requires array elements to be switchable scopes (such as countries or states). If the array contains plain numeric values (pushed in via add_to_array = { array = x value = 3 }), the engine cannot switch scope and will fail silently. For numeric values, use for_loop_effect or find_highest_in_array instead.
- Using non-existent triggers inside
limit: The limit block is a trigger context and only accepts commands from the trigger whitelist. For example, to check whether the current scope is at war, write has_war = yes — not the invented is_at_war = yes. The latter will not throw an error but always evaluates to false, causing the random pool to be unexpectedly empty.