Wiki

effect · resize_array

Definition

  • Supported scope:any
  • Supported target:none

Description

Resizes array
Example: resize_array = {
	array = array_name
	value = 42 #optional, if not specified and array grows the new elements are set to this (default 0)
	size = 3 #if higher than old size, new elements are added to end. otherwise last elements are removed to match to new size
}
#shorter usage: resize_array = { array_name = 3 }

Hands-On Notes

Hands-on notes are AI-generated and checked against the vanilla command vocabulary — treat them as a starting point, not authoritative reference. The definition above is the game's own documentation.

Hands-On Usage

resize_array is commonly used in mod scenarios where dynamic array length management is needed, such as maintaining a "candidate country list" or "task queue" across event chains. It's particularly useful when you need to truncate excess elements or expand placeholder slots at a given stage. For example, in a turn-based system, you might resize the task queue to a fixed size after each round ends, preventing unbounded array growth from degrading performance:

# Trim the mission queue to a maximum of 5 slots after each round
resize_array = {
    array = global.mission_queue
    size = 5
}

# Expand the array to 10 elements, with new elements defaulting to 0
resize_array = {
    array = global.slot_pool
    size = 10
    value = 0
}

Synergy

  • [add_to_array](/wiki/effect/add_to_array): After appending elements to an array, pair it with resize_array to cap the array size and prevent unbounded growth.
  • [remove_from_array](/wiki/effect/remove_from_array): Both can reduce array size, but resize_array excels at batch truncation from the end, while remove_from_array targets precise removal of specific values. Combine based on your needs.
  • [clear_array](/wiki/effect/clear_array): When you need a complete reset and rebuild, use clear_array first to zero it out, then resize_array to pre-allocate a fixed number of initial slots.
  • [collection_size](/wiki/trigger/collection_size): Check the current array length with collection_size before executing resize_array for conditional logic, avoiding redundant operations on empty arrays or arrays already at the target size.

Common Pitfalls

  1. Data loss at the end during reduction: When size is smaller than the current array length, trailing elements are directly discarded without any warning. Newcomers often mistakenly assume the deleted elements are "useless," but the operation simply truncates from the end in index order. Always verify that important data is positioned toward the front of the array before reducing it.
  2. Misusing shorthand syntax and losing value control: The shorthand form resize_array = { array_name = 3 } cannot specify value, so newly added elements always default to 0. If your logic requires new elements to have a specific default value (e.g., -1 for "empty slot"), you must use the full brace syntax. Otherwise, subsequent check_variable checks may fail because the default value doesn't match expectations.