Wiki

trigger · resize_temp_array

Definition

  • Supported scope:any
  • Supported target:none

Description

Resizes a temp array
Example: resize_temp_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_temp_array = { array_name = 3 }

实战 · 配合 · 坑

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

实战用法

resize_temp_array 常用于需要动态控制临时数组长度的脚本场景,例如在循环运算完成后裁剪多余元素、或在初始化阶段为数组预分配固定数量的槽位并填充默认值。典型场景包括:构建固定长度的候选国家列表、或在 for_each_scope_loop 收集完数据后截断到指定大小。

# 收集完数据后,将临时数组截断为前 3 个元素
resize_temp_array = {
    array = candidate_array
    size = 3
}

# 简写:将 my_temp_array 大小设为 5(新元素默认填 0)
resize_temp_array = { my_temp_array = 5 }

配合关系

  • [add_to_temp_array](/wiki/trigger/add_to_temp_array):先用它向数组中批量添加元素,再用 resize_temp_array 将数组裁剪或扩展到目标长度,是构建定长列表的标准组合。
  • [clear_temp_array](/wiki/trigger/clear_temp_array):在重新填充前先清空数组,配合 resize_temp_array 可实现"重置并预分配"的初始化模式。
  • [find_highest_in_array](/wiki/trigger/find_highest_in_array):对调整好大小的数组执行最大值查找,确保查找范围符合预期,避免遍历到无效的默认值元素。
  • [collection_size](/wiki/trigger/collection_size):可在 resize_temp_array 前先检查当前数组实际长度,决定是否需要扩张或收缩,使逻辑更健壮。

常见坑

  1. 缩小数组会永久丢失末尾元素:新手常以为缩小后再扩大能恢复原来的值,实际上超出新 size 的元素在缩小时已被移除,扩大后补入的只是默认值(0),务必在缩小前确认不再需要那些元素。
  2. 在 trigger 块中误用该命令resize_temp_array 本质是带副作用的操作(会修改数组状态),虽然技术上允许出现在 trigger 上下文,但在纯条件判断(如 limit/available)中调用会产生难以追踪的状态污染,应尽量将其放在 effect 块中调用。

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_temp_array is commonly used in scripting scenarios where you need dynamic control over temporary array length, such as trimming excess elements after loop operations complete, or pre-allocating a fixed number of slots during initialization and populating them with default values. Typical use cases include: building fixed-length candidate country lists, or truncating to a specified size after data collection in for_each_scope_loop.

# After data collection, truncate the temporary array to the first 3 elements
resize_temp_array = {
    array = candidate_array
    size = 3
}

# Shorthand: set my_temp_array size to 5 (new elements default to 0)
resize_temp_array = { my_temp_array = 5 }

Synergy

  • [add_to_temp_array](/wiki/trigger/add_to_temp_array): Use it to batch-add elements to the array first, then use resize_temp_array to trim or expand the array to target length—this is the standard pattern for building fixed-length lists.
  • [clear_temp_array](/wiki/trigger/clear_temp_array): Clear the array before refilling; combined with resize_temp_array, you can achieve a "reset and pre-allocate" initialization pattern.
  • [find_highest_in_array](/wiki/trigger/find_highest_in_array): Perform maximum-value lookup on the resized array to ensure the search range meets expectations and avoid traversing into invalid default-value elements.
  • [collection_size](/wiki/trigger/collection_size): Check the actual current array length before calling resize_temp_array to decide whether expansion or contraction is needed, making your logic more robust.

Common Pitfalls

  1. Shrinking the array permanently loses trailing elements: Beginners often assume that shrinking and then expanding will recover the original values. In reality, elements beyond the new size are removed during shrinking, and expansion afterward only fills in default values (0). Always confirm you no longer need those elements before shrinking.
  2. Misusing this command in trigger blocks: resize_temp_array is fundamentally an operation with side effects (it modifies array state). Although technically allowed in trigger contexts, calling it in pure condition-checking code (such as limit/available) causes hard-to-track state pollution. You should aim to invoke it within effect blocks instead.