Wiki

trigger · check_variable

Definition

  • Supported scope:any
  • Supported target:any

Description

Compares a variable to a number or another variable
ex:
check_variable = {
	var = varname
	value = 12	# accepts variables
	compare = equals
	# possible values for compare :
	# less_than, less_than_or_equals
	# greater_than, greater_than_or_equals
	# equals, not_equals
	tooltip = loc_str_id_with_LEFT_and_RIGHT  #localized text with LEFT and/or RIGHT tokens in it
}
# some shorter versions :
check_variable = { varname = 0 }
check_variable = { varname > 12 }
check_variable = { varname < 42 }
check_variable = { varname > another_varname }

实战 · 配合 · 坑

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

实战用法

check_variable 最常见于追踪玩家自定义计数器、资源变量或阶段进度的场景,例如判断某个事件链已触发次数是否达到阈值,或验证通过 set_variable 累积的"声望点数"是否足以解锁某决议。下面示例判断一个国家的"势力值"变量是否大于等于 50,才允许触发某决议:

available = {
    check_variable = {
        var = my_mod_influence_points
        value = 50
        compare = greater_than_or_equals
    }
}

配合关系

  • [has_variable](/wiki/trigger/has_variable):在使用 check_variable 前先检查变量是否已被初始化,避免因变量不存在而报错。
  • [set_variable](/wiki/effect/set_variable):通常在 effect 块中用 set_variable 写入或重置变量值,再在后续 trigger 中用 check_variable 读取比较,两者构成"写-读"闭环。
  • [add_to_variable](/wiki/effect/add_to_variable):对变量做累加操作后,配合 check_variable 检测是否达到触发条件,是计数器类设计的标准模式。
  • [if](/wiki/trigger/if):在复杂条件块中将 check_variable 嵌入 if 内,实现根据变量值分支判断不同逻辑。

常见坑

  1. 变量未初始化就直接比较:若目标变量从未被 set_variableadd_to_variable 赋过值,check_variable 读取结果为 0,但在某些上下文中会静默失败或产生非预期结果,务必在初始化事件或 on_startup 中提前赋默认值,并搭配 has_variable 做保护判断。
  2. 比较符写成等号赋值语法:新手习惯把 compare = equals 与 effect 块的赋值混淆,或在简写形式中误用 = 表示"相等比较"(实际简写相等应写 { varname = 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

check_variable is most commonly used in scenarios involving tracking player-defined counters, resource variables, or progression stages—for example, determining whether an event chain has triggered a sufficient number of times, or verifying whether accumulated "prestige points" set via set_variable are enough to unlock a particular decision. The example below checks whether a country's "influence points" variable is greater than or equal to 50 before allowing a decision to trigger:

available = {
    check_variable = {
        var = my_mod_influence_points
        value = 50
        compare = greater_than_or_equals
    }
}

Synergy

  • [has_variable](/wiki/trigger/has_variable): Check whether a variable has been initialized before using check_variable to avoid errors caused by missing variables.
  • [set_variable](/wiki/effect/set_variable): Typically use set_variable in an effect block to write or reset variable values, then read and compare them in subsequent triggers using check_variable, forming a complete "write-read" cycle.
  • [add_to_variable](/wiki/effect/add_to_variable): After performing cumulative operations on a variable, pair it with check_variable to detect whether a trigger condition has been met; this is the standard pattern for counter-based designs.
  • [if](/wiki/trigger/if): Embed check_variable within if statements in complex conditional blocks to branch logic based on variable values.

Common Pitfalls

  1. Comparing variables without initialization: If the target variable has never been assigned a value via set_variable or add_to_variable, check_variable will read it as 0, but in certain contexts this may silently fail or produce unexpected results. Always set default values in initialization events or on_startup and protect with has_variable checks.
  2. Writing comparison operators as assignment syntax: Beginners often confuse compare = equals with assignment syntax in effect blocks, or mistakenly use = in shorthand forms to mean "equals comparison" (the correct shorthand for equality is { varname = 0 }), leading to logic that doesn't match intentions. Pay careful attention to the equivalence between shorthand and full forms.