Wiki

effect · multiply_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Multiplies a variable by a value, another variable, or a [math expression](script_math_expression.md).

### Examples

multiply_variable = { num_dogs = 2 } multiply_variable = { num_dogs = { value = num_cats add = 1 } }

实战 · 配合 · 坑

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

实战用法

multiply_variable 常用于动态数值缩放场景,例如根据某项加成系数对已积累的资源变量进行乘法放大,或在科研/经济 mod 中将基础产量乘以效率倍率。下例演示将 production_output 变量乘以 1.5 的效率系数:

# 在某事件的 option 块中,将产量变量扩大为原来的 1.5 倍
multiply_variable = {
    var = production_output
    value = 1.5
}

配合关系

  • [set_variable](/wiki/effect/set_variable):通常先用 set_variable 初始化变量的基础值,再用 multiply_variable 对其做倍率调整,两者构成"赋初值→缩放"的标准流程。
  • [add_to_variable](/wiki/effect/add_to_variable):乘法与加法组合可实现"先乘后加"的复合公式,模拟带固定增量的线性缩放逻辑。
  • [check_variable](/wiki/trigger/check_variable):乘法运算后通常需要检验结果是否越过某个阈值,用 check_variable 作条件判断可防止数值失控。
  • [clamp_variable](/wiki/effect/clamp_variable):对乘法结果立即做上下界夹紧,避免极端倍率导致变量溢出或产生不合理的游戏状态。

常见坑

  1. 忘记变量未初始化就直接相乘:若目标变量从未被 set_variable 赋值,其默认值为 0,乘以任何数仍为 0,不会报错但结果完全静默地出错,新手很难察觉。
  2. value 写成变量名字符串而非数值或 var:xxx 引用value 字段若要引用另一个变量应写 value = var:some_var,直接写裸字符串会导致脚本解析失败或被当作 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

multiply_variable is commonly used in dynamic value scaling scenarios, such as amplifying accumulated resource variables by a modifier coefficient, or multiplying base output by an efficiency multiplier in research/economy mods. The following example demonstrates multiplying the production_output variable by an efficiency coefficient of 1.5:

# In an option block of an event, scale the production variable to 1.5 times its original value
multiply_variable = {
    var = production_output
    value = 1.5
}

Synergy

  • [set_variable](/wiki/effect/set_variable): Typically use set_variable first to initialize the base value of a variable, then apply multiply_variable to adjust it by a multiplier. These two form the standard "initialize → scale" workflow.
  • [add_to_variable](/wiki/effect/add_to_variable): Combining multiplication and addition enables "multiply then add" composite formulas, simulating linear scaling logic with a fixed increment.
  • [check_variable](/wiki/trigger/check_variable): After multiplication operations, you typically need to verify whether the result crosses a certain threshold. Using check_variable as a conditional check prevents numerical runaway.
  • [clamp_variable](/wiki/effect/clamp_variable): Immediately clamp the multiplication result within upper and lower bounds to prevent extreme multipliers from causing variable overflow or unrealistic game states.

Common Pitfalls

  1. Multiplying without initializing the variable first: If the target variable has never been assigned a value by set_variable, its default value is 0. Multiplying by any number still yields 0, which won't throw an error but silently produces incorrect results—easily missed by beginners.
  2. Writing value as a bare variable name string instead of a numeric literal or var:xxx reference: If the value field needs to reference another variable, write it as value = var:some_var. Using a bare string causes script parsing failure or gets treated as 0.