Wiki

trigger · find_lowest_in_array

Definition

  • Supported scope:any
  • Supported target:any

Description

Runs a loop on for each element of an array, finds the lowest value and stores result in temp variables
Example: find_lowest_in_array = {
	array = array_name
	value = value_name #optional (default 'v') lowest value in array will be stored in this temp variable
	index = index_name #optional (default 'i') index of lowest value in array will be stored in this temp variable
}

实战 · 配合 · 坑

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

实战用法

find_lowest_in_array 常用于需要从动态数组中找出最小值的 mod 场景,例如在多国援助分配逻辑中找出工业实力最低的国家对应的数值,或在经济模拟 mod 里从一组资源消耗量中定位最小值以做决策参考。注意它虽然语法形如 effect,但在条件块中使用时其核心作用是将结果写入临时变量供后续判断读取。

# 找出数组 resource_values 中的最低值,存入 temp 变量 min_val,索引存入 min_idx
find_lowest_in_array = {
    array = resource_values
    value = min_val
    index = min_idx
}
# 随后用 check_variable 读取结果
if = {
    limit = {
        check_variable = { min_val < 10 }
    }
    # 执行相应逻辑
}

配合关系

  • [check_variable](/wiki/trigger/check_variable):find_lowest_in_array 执行后,最低值结果存储在 temp 变量中,必须通过 check_variable 来读取并作真假判断,二者几乎总是成对出现。
  • [is_in_array](/wiki/trigger/is_in_array):找到最低值的索引后,可用 is_in_array 验证某个特定元素是否处于目标数组中,辅助做进一步范围校验。
  • [add_to_array](/wiki/effect/add_to_array):通常在调用 find_lowest_in_array 之前,需要先用 add_to_array 动态构建或填充被遍历的数组。
  • [find_highest_in_array](/wiki/trigger/find_highest_in_array):与本命令互为镜像,常同时使用以获得数组的值域范围(最大值与最小值),用于区间判断或归一化计算。

常见坑

  1. 忘记 temp 变量的生命周期valueindex 存储的是 temp 变量,仅在当前事件/效果执行链内有效。新手容易在另一个独立事件中试图读取这些值,导致变量为空或读到上次残留的无关数值,应确保读取逻辑与本命令在同一执行上下文内。
  2. 对空数组调用导致未定义行为:如果目标数组为空,valueindex 变量将不会被写入有意义的值,后续 check_variable 可能因变量不存在而直接返回假。应在调用前用 [collection_size](/wiki/trigger/collection_size) 检查数组长度大于 0,确保数组非空再执行查找。

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

find_lowest_in_array is commonly used in mod scenarios requiring extraction of minimum values from dynamic arrays—for example, identifying the country with the lowest industrial capacity in multi-nation aid distribution logic, or locating the minimum resource consumption value from a set of consumption figures in an economic simulation mod for decision-making reference. Note that while its syntax resembles an effect, when used in condition blocks its primary function is to write the result into a temporary variable for subsequent conditional evaluation.

# Find the lowest value in the resource_values array, store it in temp variable min_val, and index in min_idx
find_lowest_in_array = {
    array = resource_values
    value = min_val
    index = min_idx
}
# Subsequently read the result with check_variable
if = {
    limit = {
        check_variable = { min_val < 10 }
    }
    # Execute corresponding logic
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): After find_lowest_in_array executes, the lowest value result is stored in a temp variable and must be read and evaluated as true/false via check_variable; the two almost always appear in tandem.
  • [is_in_array](/wiki/trigger/is_in_array): Once the index of the lowest value is found, use is_in_array to validate whether a specific element exists in the target array, assisting with further range validation.
  • [add_to_array](/wiki/effect/add_to_array): Typically before invoking find_lowest_in_array, you need to dynamically construct or populate the array to be traversed using add_to_array.
  • [find_highest_in_array](/wiki/trigger/find_highest_in_array): The mirror counterpart to this command, commonly used together to obtain the value range of an array (maximum and minimum values) for interval comparison or normalization calculations.

Common Pitfalls

  1. Forgetting the lifetime scope of temp variables: The value and index store temp variables that are valid only within the current event/effect execution chain. Beginners often attempt to read these values in a separate independent event, resulting in empty variables or stale unrelated values from previous executions. Ensure that read logic and this command share the same execution context.
  2. Invoking on empty arrays causes undefined behavior: If the target array is empty, the value and index variables will not be written with meaningful values, and subsequent check_variable may simply return false if the variable does not exist. Check array length greater than 0 using [collection_size](/wiki/trigger/collection_size) before calling to ensure the array is non-empty before performing the lookup.