trigger · clear_temp_array
Definition
- Supported scope:
any - Supported target:
none
Description
Clears the contents of a temporary array
Example: clear_temp_array = array_name
clear_temp_arrayanynoneClears the contents of a temporary array
Example: clear_temp_array = array_name
实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。
clear_temp_array 常见于需要在循环或多阶段计算中复用同一个临时数组名的场景,例如先收集满足条件的国家列表、处理完毕后清空,再进行下一轮收集。典型应用包括动态事件选项生成、按条件筛选州省列表等复杂数组运算流程中的"重置"步骤。
# 在 effect 块中先用完数组,再清空以供后续逻辑复用
effect = {
# ... 对 target_countries 数组的处理逻辑 ...
clear_temp_array = target_countries # 清空,避免上轮数据污染下轮结果
# 重新向 target_countries 填入新数据
for_each_scope_loop = {
array = faction_members
add_to_temp_array = { array = target_countries val = THIS }
}
}
[add_to_temp_array](/wiki/effect/add_to_temp_array):最直接的搭档——先用 add_to_temp_array 向数组写入元素,处理完毕后用 clear_temp_array 清除,为下一轮写入提供干净的起点。[for_each_scope_loop](/wiki/effect/for_each_scope_loop):循环体执行前若需要重新构造数组,常在循环外先调用 clear_temp_array 保证数组为空,避免重复累积。[remove_from_temp_array](/wiki/effect/remove_from_temp_array):当只需要移除部分元素时用 remove_from_temp_array,而需要彻底重置时则换用 clear_temp_array,两者形成"局部清理 vs 全量清理"的互补关系。[is_in_array](/wiki/trigger/is_in_array):在清空数组前往往会用 is_in_array 做最后一次查询判断,两者构成"用完即清"的标准模式。clear_temp_array 只能清空以 temp_ 开头(或在同一 effect 块内声明的临时)数组;若想清空通过 set_variable 等持久化的普通数组,应使用 [clear_array](/wiki/effect/clear_array),混用会导致脚本报错或逻辑静默失效。for_each_scope_loop 或 while_loop_effect 内部调用 clear_temp_array 会在每次迭代都把数组清空,通常应将清空操作放在循环之外(循环开始前或结束后),否则循环内积累的数据会被反复抹除,造成难以追踪的逻辑错误。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.
clear_temp_array is commonly used in scenarios where you need to reuse the same temporary array name across loops or multi-stage calculations—for example, collecting a list of countries that meet certain conditions, processing them, clearing the array, and then performing another collection pass. Typical applications include dynamic event option generation, filtering state/province lists by criteria, and other complex array operations that require a "reset" step in the workflow.
# In an effect block, use the array first, then clear it to allow subsequent logic to reuse it
effect = {
# ... processing logic for target_countries array ...
clear_temp_array = target_countries # Clear to prevent previous iteration data from polluting the next result
# Re-populate target_countries with fresh data
for_each_scope_loop = {
array = faction_members
add_to_temp_array = { array = target_countries val = THIS }
}
}
[add_to_temp_array](/wiki/effect/add_to_temp_array): The most direct partner—use add_to_temp_array to write elements into the array, and after processing, use clear_temp_array to remove them, providing a clean slate for the next write operation.[for_each_scope_loop](/wiki/effect/for_each_scope_loop): If the array needs to be reconstructed before loop execution, commonly invoke clear_temp_array outside the loop beforehand to ensure the array is empty and prevent duplicate accumulation.[remove_from_temp_array](/wiki/effect/remove_from_temp_array): Use remove_from_temp_array when you only need to remove certain elements, but switch to clear_temp_array when you need a complete reset. These two form a complementary relationship of "partial cleanup vs. full cleanup".[is_in_array](/wiki/trigger/is_in_array): Before clearing the array, you typically perform one last query check with is_in_array. Together they constitute the standard pattern of "use and then clear".clear_temp_array can only clear arrays that begin with temp_ (or temporary arrays declared within the same effect block). If you want to clear ordinary arrays persisted via set_variable and similar methods, use [clear_array](/wiki/effect/clear_array) instead. Mixing these up causes script errors or silent logic failures.clear_temp_array inside for_each_scope_loop or while_loop_effect will wipe the array on every iteration, which usually results in data loss. The clear operation should be placed outside the loop (before it starts or after it ends). Otherwise, data accumulated within the loop will be repeatedly erased, causing hard-to-trace logic bugs.