Wiki

effect · 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 常用于需要从一组动态数值中找出最大值的场景,例如:在多国博弈 mod 中比较各国的某项资源、工厂数或分数,找出领先的国家索引后再据此触发特殊事件或奖励。典型用法是先用 add_to_array 填充一个数值数组,再用本命令扫描出最高值及其位置。

# 假设已将各国得分写入 scores_array
find_highest_in_array = {
    array = scores_array
    value = best_score   # 最高分存入 temp 变量 best_score
    index = best_index   # 最高分的下标存入 temp 变量 best_index
}
# 之后可用 best_score / best_index 驱动后续逻辑
if = {
    limit = { check_variable = { best_score > 100 } }
    set_variable = { winner_index = best_index }
}

配合关系

  • [add_to_array](/wiki/effect/add_to_array) — 在调用本命令之前用它向目标数组逐条写入数值,是数组数据的来源。
  • [find_lowest_in_array](/wiki/effect/find_lowest_in_array) — 与本命令对称使用,可同时取得最大值与最小值,方便计算值域或排名区间。
  • [check_variable](/wiki/trigger/check_variable) — 遍历结束后用它对存储结果的 temp 变量做条件判断,决定后续分支逻辑。
  • [for_each_loop](/wiki/effect/for_each_loop) — 常在本命令之前配合使用,批量计算并填充数组内的各元素值,为查找最高值做准备。

常见坑

  1. 忘记 value / index 使用的是临时变量:省略这两个字段时默认使用 vi,若同一执行块内其他循环(如 for_each_loop)也在使用默认迭代变量,会互相覆盖,导致结果不可预期——务必为不同的查找操作指定唯一的变量名。
  2. 对空数组调用本命令:若数组在执行时为空(长度为 0),valueindex 将不会被写入任何有意义的值,后续用 check_variable 读取它们时可能得到上一次遗留的旧数据或 0,应在调用前先用 [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—for example, in multi-country competition mods comparing a resource metric, factory count, or score across nations, then triggering special events or rewards based on the index of the leading country. The typical workflow is to first populate a numerical array using add_to_array, then scan it with this command to obtain the highest value and its position.

# Assuming country scores have been written to scores_array
find_highest_in_array = {
    array = scores_array
    value = best_score   # Highest score stored in temp variable best_score
    index = best_index   # Index of highest score stored in temp variable best_index
}
# Subsequently, best_score / best_index can drive your logic
if = {
    limit = { check_variable = { best_score > 100 } }
    set_variable = { winner_index = best_index }
}

Synergy

  • [add_to_array](/wiki/effect/add_to_array) — Use this before calling the present command to write values into the target array one by one; it is the data source for your array.
  • [find_lowest_in_array](/wiki/effect/find_lowest_in_array) — Use in tandem with this command to obtain both the maximum and minimum values simultaneously, convenient for calculating ranges or ranking intervals.
  • [check_variable](/wiki/trigger/check_variable) — After the scan completes, use it to perform conditional checks on the temp variables storing results, deciding subsequent branching logic.
  • [for_each_loop](/wiki/effect/for_each_loop) — Often paired with this command beforehand to compute and populate each element in the array, preparing it for the highest-value lookup.

Common Pitfalls

  1. Forgetting that value / index use temporary variables: When these two fields are omitted, the defaults are v and i. If other loops in the same execution block (such as for_each_loop) also use the default iteration variables, they will overwrite each other, producing unexpected results—always assign unique variable names for different lookup operations.
  2. Calling this command on an empty array: If the array is empty (length 0) at execution time, value and index will not be written to any meaningful values. Subsequently reading them with check_variable may return stale data from a previous iteration or 0—verify the array is non-empty beforehand using [collection_size](/wiki/trigger/collection_size) or logical guards.