Wiki

trigger · add_to_temp_array

Definition

  • Supported scope:any
  • Supported target:none

Description

Adds an element to a temporary array
Example: add_to_temp_array = {
	array = array_name
	value = 42 #optional, if not defined adds scope
	index = 3 #optional, default is end. otherwise elements are shifted
}
#shorter usage: add_to_temp_array = { array_name = 42 }

实战 · 配合 · 坑

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

实战用法

add_to_temp_array 在 trigger 块中最常见的场景是配合 collection_sizeis_in_array 进行条件判断前的临时数据收集,例如在 if 条件块内先将满足某条件的国家或数值暂存到临时数组,再在后续 trigger 中统计或查询该数组。由于临时数组仅存活于当前 effect/trigger 执行周期,非常适合做一次性的中间结果暂存。

# 示例:在 trigger 中将当前变量值添加到临时数组,再判断数组是否包含目标值
if = {
    limit = {
        add_to_temp_array = { array = my_temp result = 1 }
        is_in_array = { array = my_temp value = 1 }
    }
    # 满足条件后的逻辑
}

配合关系

  • [is_in_array](/wiki/trigger/is_in_array):向临时数组添加元素后,立即用 is_in_array 检查某值是否存在于其中,是最典型的"写后即查"模式。
  • [collection_size](/wiki/trigger/collection_size):将多个值逐步压入临时数组后,用 collection_size 统计数量来做阈值判断,常用于"满足条件的项目数量达到 N 个"的逻辑。
  • [clear_temp_array](/wiki/trigger/clear_temp_array):在同一 trigger 块内重复使用同名临时数组时,需先清空再写入,防止上一次残留数据干扰判断结果。
  • [find_highest_in_array](/wiki/trigger/find_highest_in_array):填充完临时数组后,用 find_highest_in_array 取出最大值赋给临时变量,再做数值比较,适合排序筛选场景。

常见坑

  1. 误把临时数组当持久存储add_to_temp_array 写入的数组在当前脚本执行结束后即消失,不能跨 event option、不能跨 on_action 持久保存。需要跨帧或跨事件保留的数据必须改用 add_to_array(effect 侧)配合普通变量。
  2. 在纯 trigger 上下文中混淆 effect 写法add_to_temp_array 虽然出现在 trigger 白名单中,但其行为本质上是"写操作",在只读的 limit 块内滥用可能导致预期外的副作用或脚本报错;应明确区分它被允许出现的上下文,避免与同名 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

add_to_temp_array is most commonly used in trigger blocks for temporary data collection before condition checks via collection_size or is_in_array. A typical pattern involves collecting countries or values meeting certain criteria into a temporary array within an if conditional block, then performing statistical or query operations on that array in subsequent triggers. Since temporary arrays persist only for the current effect/trigger execution cycle, they are ideal for one-time intermediate result storage.

# Example: Add current variable value to temporary array in trigger, then check if array contains target value
if = {
    limit = {
        add_to_temp_array = { array = my_temp result = 1 }
        is_in_array = { array = my_temp value = 1 }
    }
    # Logic after condition is met
}

Synergy

  • [is_in_array](/wiki/trigger/is_in_array): Immediately after adding elements to a temporary array, use is_in_array to check if a value exists within it—this is the classic "write-then-query" pattern.
  • [collection_size](/wiki/trigger/collection_size): After progressively pushing multiple values into a temporary array, use collection_size to count the quantity for threshold checks, commonly used in "item count reaching N" logic.
  • [clear_temp_array](/wiki/trigger/clear_temp_array): When reusing the same-named temporary array within a single trigger block, clear it first before writing to prevent stale data from interfering with judgment results.
  • [find_highest_in_array](/wiki/trigger/find_highest_in_array): After populating a temporary array, use find_highest_in_array to extract the maximum value into a temporary variable for numerical comparison, suitable for sorting and filtering scenarios.

Common Pitfalls

  1. Treating temporary arrays as persistent storage: Arrays written via add_to_temp_array disappear after the current script execution ends and cannot be preserved across event options or on_action contexts. Data that must persist across frames or events should instead use add_to_array (effect-side) paired with standard variables.
  2. Confusing effect syntax within pure trigger contexts: Although add_to_temp_array appears in trigger whitelists, its behavior is fundamentally a "write operation." Misusing it in read-only limit blocks may cause unexpected side effects or script errors. Always clarify the contexts where it is permitted and avoid conflating it with its effect-side counterpart.