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
}

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.