Wiki

trigger · clamp_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

clamps a temp variable between two values/variables

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

clamp_temp_variable is commonly used in value calculation scenarios in mods, such as economic systems, population simulations, or custom war fatigue calculations. After computations are complete, it forcibly restricts temporary variables within a reasonable range to prevent numeric overflow and subsequent logic errors. For example, when calculating industrial output bonuses for a country, ensure the result stays within both lower and upper bounds:

# Complete addition operation first, then clamp the result
add_to_temp_variable = { temp_output = some_bonus }
clamp_temp_variable = {
    var = temp_output
    min = 0
    max = 100
}

Synergy

  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): The most common prerequisite operation. Perform addition/subtraction on the temporary variable first, then use clamp to constrain the result and prevent cumulative overflow.
  • [set_temp_variable](/wiki/effect/set_temp_variable): The standard way to initialize temporary variables. Combined with clamp, it forms a complete workflow of "assignment → calculation → clamping".
  • [check_variable](/wiki/trigger/check_variable): Use after clamp to verify whether the clamped value meets specific conditions, thus determining subsequent branching logic.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable): Multiplication operations can cause values to expand rapidly. Using clamp immediately after is a common safeguard.

Common Pitfalls

  1. Mistakenly writing it as an effect outside trigger blocks: clamp_temp_variable can be used in both trigger and effect contexts, but their syntactic positions differ. Beginners often place it in a pure effect block as a regular effect while forgetting it's equally valid in trigger blocks, leading to scope confusion or missing necessary condition wrappers.
  2. Failing to distinguish variable references from literal values in min/max: If min or max are themselves variables, use correct variable reference syntax (e.g., min = var:some_var). Writing a variable name without a reference prefix will be parsed by the engine as the literal number 0, causing the clamp range to be completely different from expected.