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
}

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.