Wiki

effect · clear_temp_array

Definition

  • Supported scope:any
  • Supported target:none

Description

Clears the contents of a temporary array
Example: clear_temp_array = array_name

实战 · 配合 · 坑

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

实战用法

clear_temp_array 常用于需要在同一事件或 effect 块中复用同名临时数组的场景,例如先用数组收集一批国家或州,处理完毕后清空再重新填充,避免上一轮残留数据污染结果。在循环类脚本(如统计满足条件的国家数量)中,每次进入新的判断分支前清空数组可确保数据干净。

# 先收集所有符合条件的州,处理后清空以便复用
every_state = {
    limit = { is_controlled_by = ROOT }
    add_to_temp_array = { array = controlled_states val = THIS }
}
find_highest_in_array = { array = controlled_states out = best_state }
# 处理完毕,清空以便后续逻辑复用同名数组
clear_temp_array = controlled_states

配合关系

  • [add_to_temp_array](/wiki/effect/add_to_temp_array):最直接的搭档,清空数组后立刻重新向其填充元素,构成「重置→重建」的标准流程。
  • [for_each_scope_loop](/wiki/effect/for_each_scope_loop):遍历数组并执行逻辑后调用 clear_temp_array 释放数组,防止多次触发事件时数据累积。
  • [resize_temp_array](/wiki/effect/resize_temp_array):有时在清空后需要预分配固定大小的槽位,两者常配合完成数组的完整初始化。
  • [collection_size](/wiki/trigger/collection_size):在清空前可用此触发器检查数组是否非空,避免对空数组做多余操作,逻辑更严谨。

常见坑

  1. 误用 clear_array 而非 clear_temp_arrayclear_array 清除的是持久变量数组(保存在存档中),而 clear_temp_array 只作用于当前执行帧的临时数组,两者不可混用;用错会导致意外清除存档数据或脚本报错。
  2. 以为 temp array 会自动清空:临时数组在同一事件/effect 执行链内是持续存在的,并不会在每个 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

clear_temp_array is commonly used in scenarios where you need to reuse a temporary array with the same name within a single event or effect block. For example, you might use an array to collect a batch of countries or states, process them, then clear and refill the array to avoid pollution from residual data in the previous iteration. In looping scripts (such as counting countries that meet certain conditions), clearing the array before entering each new conditional branch ensures clean data.

# Collect all states meeting the condition first, then clear after processing to allow array reuse
every_state = {
    limit = { is_controlled_by = ROOT }
    add_to_temp_array = { array = controlled_states val = THIS }
}
find_highest_in_array = { array = controlled_states out = best_state }
# Processing complete, clear to allow subsequent logic to reuse the same-named array
clear_temp_array = controlled_states

Synergy

  • [add_to_temp_array](/wiki/effect/add_to_temp_array): The most direct companion—immediately repopulate the array after clearing it, forming the standard "reset→rebuild" workflow.
  • [for_each_scope_loop](/wiki/effect/for_each_scope_loop): After iterating through the array and executing logic, call clear_temp_array to release the array and prevent data accumulation when the event is triggered multiple times.
  • [resize_temp_array](/wiki/effect/resize_temp_array): Sometimes after clearing you need to preallocate slots of a fixed size; the two are often used together to complete full array initialization.
  • [collection_size](/wiki/trigger/collection_size): Before clearing, you can use this trigger to check if the array is non-empty, avoiding redundant operations on empty arrays and making your logic more rigorous.

Common Pitfalls

  1. Mistakenly using clear_array instead of clear_temp_array: clear_array clears persistent variable arrays (saved in the save file), while clear_temp_array only affects temporary arrays in the current execution frame. The two cannot be used interchangeably; using the wrong one will either unexpectedly erase save data or cause script errors.
  2. Assuming temp arrays automatically clear: Temporary arrays persist within the same event/effect execution chain and do not automatically reset at the start of each effect block. If you populate the same-named array multiple times in the same chain without manually clearing it, elements will continuously accumulate, causing duplicate calculations or array overflow exceptions.