Wiki

trigger · find_highest_in_array

Definition

  • Supported scope:any
  • Supported target:any

Description

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

实战 · 配合 · 坑

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

实战用法

find_highest_in_array 常用于需要从一组动态数值中找出最大值的场景,例如比较多个国家的某项资源量、从一批临时计算结果中取最高分、或在自定义科技/事件系统中找出得分最高的候选项。以下示例演示在一组存储了各省份抵抗值的数组中找出最高值并记录其下标:

find_highest_in_array = {
    array = province_resistance_values
    value = highest_resistance   # 最高值存入 highest_resistance
    index = highest_index        # 对应下标存入 highest_index
}
check_variable = {
    var = highest_resistance
    value = 50
    compare = greater_than
}

配合关系

  • [add_to_array](/wiki/effect/add_to_array) / [add_to_temp_array](/wiki/effect/add_to_temp_array):在执行 find_highest_in_array 前,需要先用这两个命令向数组填充数值,没有数据数组查找毫无意义。
  • [check_variable](/wiki/trigger/check_variable):找到最高值后,通常立即用 check_variable 对存储结果的临时变量进行阈值判断,是最常见的后续条件检验手段。
  • [is_in_array](/wiki/trigger/is_in_array):在确认最高值对应的下标或元素后,可配合 is_in_array 验证该元素是否属于某个特定集合,实现更精细的筛选逻辑。
  • [find_lowest_in_array](/wiki/effect/find_lowest_in_array):常与 find_highest_in_array 配对使用,同时获取最大值与最小值,用于计算极差或做双端比较。

常见坑

  1. 忽略默认变量名冲突valueindex 字段若不显式指定,默认分别写入临时变量 vi。这两个名称极为通用,如果外层循环(如 for_each_loop)已经在使用 v/i,调用后会静默覆盖原有值,导致外层循环逻辑错乱,务必手动指定唯一的变量名。
  2. 对空数组调用行为未定义:如果目标数组在调用时尚未赋值或长度为零,find_highest_in_array 不会报错,但结果变量的值是不可预期的(可能保留上次赋值或为零),直接对其做 check_variable 判断会产生错误逻辑,应在调用前用 [collection_size](/wiki/trigger/collection_size) 确认数组非空。

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_highest_in_array is commonly used in scenarios where you need to extract the maximum value from a set of dynamic numbers—such as comparing a specific resource across multiple countries, finding the highest score from a batch of temporary calculation results, or identifying the top-scoring candidate in a custom technology or event system. The following example demonstrates finding the maximum value from an array storing province resistance values and recording its index:

find_highest_in_array = {
    array = province_resistance_values
    value = highest_resistance   # Store the highest value in highest_resistance
    index = highest_index        # Store the corresponding index in highest_index
}
check_variable = {
    var = highest_resistance
    value = 50
    compare = greater_than
}

Synergy

  • [add_to_array](/wiki/effect/add_to_array) / [add_to_temp_array](/wiki/effect/add_to_temp_array): Before executing find_highest_in_array, you must first populate the array with values using these commands; searching an empty array is meaningless.
  • [check_variable](/wiki/trigger/check_variable): After finding the highest value, it is standard practice to immediately use check_variable to perform a threshold check on the temporary variable storing the result—this is the most common subsequent condition validation approach.
  • [is_in_array](/wiki/trigger/is_in_array): After confirming the index or element corresponding to the highest value, you can pair it with is_in_array to verify whether that element belongs to a specific set, enabling more granular filtering logic.
  • [find_lowest_in_array](/wiki/effect/find_lowest_in_array): Often paired with find_highest_in_array to retrieve both maximum and minimum values simultaneously, useful for calculating range or performing dual-end comparisons.

Common Pitfalls

  1. Overlooking default variable name conflicts: If the value and index fields are not explicitly specified, they default to writing into temporary variables v and i respectively. These names are extremely generic, and if an outer loop (such as for_each_loop) is already using v/i, the call will silently overwrite the original values, corrupting the outer loop's logic. Always manually specify unique variable names.
  2. Undefined behavior when calling on empty arrays: If the target array is uninitialized or has zero length when called, find_highest_in_array will not throw an error, but the resulting variable's value is unpredictable (it may retain a previous value or be zero). Performing a direct check_variable check on it will produce incorrect logic. Always confirm the array is non-empty using [collection_size](/wiki/trigger/collection_size) before calling.