Wiki

effect · subtract_from_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Subtracts a value, a variable, or a [math expression](script_math_expression.md) from a variable.

### Examples

subtract_from_variable = { num_dogs = 5 } subtract_from_variable = { num_dogs = { value = num_cats multiply = 2 } }

实战 · 配合 · 坑

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

实战用法

subtract_from_variable 常见于动态资源管理系统,例如自定义"国家积分"或"政治资本"机制,在玩家选择某个 option 时扣减对应变量值。配合 tooltip 参数可以向玩家直观展示扣减前后的数值变化,提升 mod 的 UI 友好度。

# 玩家选择"发动宣传攻势"时扣减政治资本
subtract_from_variable = {
    var = political_capital
    value = 25
    tooltip = tt_propaganda_cost  # 本地化文本中含 LEFT/RIGHT 标记
}

配合关系

  • [check_variable](/wiki/trigger/check_variable):在执行扣减前先判断变量是否满足最低阈值,防止变量被减成负数导致逻辑异常。
  • [clamp_variable](/wiki/effect/clamp_variable):扣减之后用来把变量钳制在合法区间,是资源系统的安全兜底手段。
  • [add_to_variable](/wiki/effect/add_to_variable):与本命令互为逆操作,组合使用可实现"消耗与回充"的完整资源循环。
  • [set_variable](/wiki/effect/set_variable):当需要将变量重置为某个基准值而非相对扣减时,与本命令形成补充关系。

常见坑

  1. 忘记初始化变量就直接扣减:若目标变量尚未被 set_variableadd_to_variable 赋予初始值,游戏会将其视为 0 后再减,导致出现非预期的负值;应在 on_startup 或国家 focus 完成时确保变量已被初始化。
  2. var 填写了不存在的变量名或拼写错误:HOI4 脚本对变量名不做强制校验,拼错后既不报错也不生效,排查时极难发现;建议统一用命名前缀(如 mymod_capital)并在整个 mod 内保持一致。

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

subtract_from_variable is commonly used in dynamic resource management systems, such as custom "national points" or "political capital" mechanics, to deduct variable values when players select certain options. Combined with the tooltip parameter, you can provide players with intuitive visual feedback of value changes before and after the subtraction, enhancing the UI friendliness of your mod.

# Deduct political capital when player chooses "Launch Propaganda Campaign"
subtract_from_variable = {
    var = political_capital
    value = 25
    tooltip = tt_propaganda_cost  # Localization text contains LEFT/RIGHT markers
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): Validate that the variable meets a minimum threshold before executing the subtraction to prevent the variable from becoming negative and causing logic errors.
  • [clamp_variable](/wiki/effect/clamp_variable): After subtraction, clamp the variable to a valid range as a safety fallback for your resource system.
  • [add_to_variable](/wiki/effect/add_to_variable): Acts as the inverse operation of this command; combining them enables a complete resource cycle of "consumption and replenishment".
  • [set_variable](/wiki/effect/set_variable): When you need to reset a variable to a baseline value rather than applying a relative deduction, it complements this command.

Common Pitfalls

  1. Forgetting to initialize the variable before deducting: If the target variable has not been assigned an initial value via set_variable or add_to_variable, the game will treat it as 0 and then subtract, resulting in unexpected negative values. Ensure variables are initialized during on_startup or when a country focus is completed.
  2. Using a non-existent variable name or misspelling the var field: HOI4 scripting does not enforce validation on variable names, so typos will silently fail without error messages, making them extremely difficult to debug. It is recommended to use a consistent naming prefix (such as mymod_capital) and maintain consistency throughout your mod.