命令百科

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) 或逻辑保护确认数组非空。