trigger · clamp_temp_variable
Definition
- Supported scope:
any - Supported target:
none
Description
clamps a temp variable between two values/variables
clamp_temp_variableanynoneclamps a temp variable between two values/variables
实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。
clamp_temp_variable 常用于数值计算类 mod 场景,例如经济系统、人口模拟或自定义战争疲劳度计算,在运算结束后将临时变量强制限制在合理区间内,防止数值越界导致后续逻辑异常。例如,在计算某国工业产出加成时,确保结果不低于下限也不超过上限:
# 先完成加法运算,再钳制结果
add_to_temp_variable = { temp_output = some_bonus }
clamp_temp_variable = {
var = temp_output
min = 0
max = 100
}
[add_to_temp_variable](/wiki/effect/add_to_temp_variable):最常见的前置操作,先对临时变量做加减运算,再用 clamp 约束结果,防止累加溢出。[set_temp_variable](/wiki/effect/set_temp_variable):初始化临时变量的标准方式,与 clamp 配合构成「赋值 → 运算 → 钳制」的完整流程。[check_variable](/wiki/trigger/check_variable):在 clamp 之后使用,验证钳制后的值是否满足特定条件,从而决定后续分支逻辑。[multiply_temp_variable](/wiki/effect/multiply_temp_variable):乘法运算后数值可能急剧膨胀,紧接着用 clamp 是常见的防护手段。clamp_temp_variable 既可用于 trigger 上下文也可用于 effect 上下文,但两者语法位置不同,初学者容易把它写在纯 effect 块里当普通 effect 使用却忘记它在 trigger 块中同样合法,导致混淆 scope 或漏掉必要的条件包裹。min 或 max 本身也是变量,需要使用正确的变量引用语法(如 min = var:some_var),直接写变量名而不加引用前缀会被引擎解析为字面数字 0,导致钳制范围与预期完全不符。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.
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
}
[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.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.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.