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 }

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

resize_array 常用于动态管理数组长度的 mod 场景,例如在事件链中维护一个"候选国家列表"或"任务队列",需要在某一阶段截断多余元素或扩充占位槽时使用。比如在一个轮次制系统中,每轮结束后将记录数组裁减到固定大小,避免数组无限增长拖慢性能:

# 每轮结束后把任务队列裁剪到最多 5 个槽位
resize_array = {
    array = global.mission_queue
    size = 5
}

# 扩充数组到 10 个元素,新元素默认填 0
resize_array = {
    array = global.slot_pool
    size = 10
    value = 0
}

配合关系

  • [add_to_array](/wiki/effect/add_to_array):向数组追加元素后,配合 resize_array 限制数组上限,防止数组无节制增长。
  • [remove_from_array](/wiki/effect/remove_from_array):两者都可以缩减数组,但 resize_array 适合批量截断末尾,remove_from_array 适合精确删除指定值;根据需求组合使用。
  • [clear_array](/wiki/effect/clear_array):当需要彻底清空再重建时,先 clear_array 清零,再用 resize_array 预分配固定大小的初始槽位。
  • [collection_size](/wiki/trigger/collection_size):在执行 resize_array 之前用 collection_size 检查当前数组长度,可做条件判断,避免对空数组或已符合大小的数组做无意义操作。

常见坑

  1. 缩减时丢失末尾数据size 小于当前数组长度时,末尾元素会被直接丢弃而不会触发任何警告,新手常误以为删除的是"无用的"元素,实际上是按索引顺序从末尾截断,务必在缩减前确认重要数据已存储在数组靠前的位置。
  2. 误用简短语法覆盖 value:简短写法 resize_array = { array_name = 3 } 无法指定 value,新增元素固定为 0;若业务逻辑要求新元素为特定默认值(如 -1 表示"空槽"),必须使用完整的花括号语法,否则后续 check_variable 判断会因默认值不符预期而出错。

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.