Wiki

effect · clamp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Clamps a variable between two values or variables.
Note that either min or max can be omitted.
The order in which the operations are applied is Max( Min( var, max ), min ).
An error will be logged if max < min as the result will be more often than not undesired (requires the game to run in debug mode).
Example: clamp_variable = {
    var = num_dogs
    min = 0
    max = num_cats
}

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

clamp_variable 常用于需要对动态变量做边界保护的场景,例如经济系统中的资源值、自定义势力好感度或人口数量,防止其因连续加减操作而超出合理范围(如负值或溢出上限)。典型场景是玩家触发事件后对某变量做修正,紧接着立刻夹断以保证其合法性。

# 每月脉冲:对玩家的"民心值"进行加减后,将其约束在 0~100 之间
add_to_variable = { var = popular_support value = stability_bonus }
clamp_variable = {
    var = popular_support
    min = 0
    max = 100
}

配合关系

  • [add_to_variable](/wiki/effect/add_to_variable) / [subtract_from_variable](/wiki/effect/subtract_from_variable):对变量做算术操作后,立即用 clamp_variable 防止结果越界,是最典型的"改 → 夹"组合。
  • [set_variable](/wiki/effect/set_variable):在初始化赋值时若来源数据不可控(如从其他变量拷贝),可随后夹断确保初始值合法。
  • [check_variable](/wiki/trigger/check_variable):在 if 块中先判断变量当前范围,再决定是否还需要 clamp,避免不必要的触发;也可用于调试时验证 clamp 是否生效。
  • [multiply_variable](/wiki/effect/multiply_variable):乘法运算后数值可能急剧放大,之后配合 clamp_variable 做上限保护尤为必要。

常见坑

  1. min 大于 max 时结果反直觉:官方说明执行顺序是 Max( Min(var, max), min ),若 min > max,最终结果将始终等于 min 而非预期中间值,且只在 debug 模式下才会输出错误日志,Release 版本下会静默产生错误行为,务必确保 min ≤ max
  2. 误用字面量代替变量名min/max 既可以是数字字面量也可以是变量名,但变量名必须是当前 scope 下已存在且有值的变量;若引用了一个尚未初始化(不存在)的变量作为边界,该边界将被当作 0 处理,导致夹断行为与预期完全不符,且不会有明显报错提示。

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_variable is commonly used in scenarios where dynamic variables require boundary protection, such as resource values in economic systems, custom faction favorability, or population counts, preventing them from exceeding reasonable ranges (such as negative values or hitting upper limits) due to successive add/subtract operations. A typical scenario is when a player triggers an event to modify a variable, then immediately clamps it to ensure its validity.

# Monthly pulse: after adding/subtracting from the player's "popular_support", constrain it between 0~100
add_to_variable = { var = popular_support value = stability_bonus }
clamp_variable = {
    var = popular_support
    min = 0
    max = 100
}

Synergy

  • [add_to_variable](/wiki/effect/add_to_variable) / [subtract_from_variable](/wiki/effect/subtract_from_variable): After performing arithmetic operations on a variable, immediately use clamp_variable to prevent result overflow—this is the most typical "modify → clamp" combination.
  • [set_variable](/wiki/effect/set_variable): When initializing values from uncontrollable sources (such as copying from other variables), you can subsequently clamp to ensure the initial value is valid.
  • [check_variable](/wiki/trigger/check_variable): Before deciding whether additional clamping is needed, first check the current range of the variable in an if block to avoid unnecessary triggers; can also be used during debugging to verify that clamp is working as expected.
  • [multiply_variable](/wiki/effect/multiply_variable): Multiplication operations can cause values to expand dramatically, making it especially necessary to pair with clamp_variable for upper-bound protection afterward.

Common Pitfalls

  1. Counterintuitive results when min exceeds max: According to official documentation, the execution order is Max( Min(var, max), min ). If min > max, the final result will always equal min rather than an expected intermediate value. Error logs are only output in debug mode; Release builds silently produce incorrect behavior. Always ensure min ≤ max.
  2. Mistakenly using literal values instead of variable names: Both min/max can be numeric literals or variable names, but variable names must be existing variables in the current scope with assigned values. If you reference a variable that has not been initialized (does not exist) as a boundary, that boundary will be treated as 0, causing clamping behavior completely at odds with expectations, and no obvious error will be reported.