Wiki

effect · randomize_temp_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Randomize a temporary variable
randomize_temp_variable = num_dogs
# which is equivalient to
randomize_temp_variable = {
  var = num_dogs
  distribution = uniform
}
# which is equivalent to
randomize_temp_variable = {
  var = num_dogs
  distribution = uniform
  min = 0
  max = 1
}
# also allow for binomial distribution (with N=2)randomize_temp_variable = {
  var = num_dogs
  distribution = binomial
  min = 0               # optional
  max = 10              # required if min is specified
}
# also allow for the poisson distributionrandomize_temp_variable = {
  var = num_dogs
  distribution = poisson
  lambda = 10           # required
  min = 10              # optional
}

实战 · 配合 · 坑

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

实战用法

randomize_temp_variable 常用于需要引入随机性的事件结果中,例如随机化战斗损失数量、随机生成资源奖励点数或决定随机事件分支权重。相比直接写死数值,它能让 mod 的结果更具不可预测性,提升游玩体验。

# 在事件选项中随机生成一个奖励政治点数(0~5 之间的均匀分布)
option = {
    name = my_event.option_a
    randomize_temp_variable = {
        var = temp_pp_gain
        distribution = uniform
        min = 0
        max = 5
    }
    round_temp_variable = temp_pp_gain
    add_political_power = temp_pp_gain
}

配合关系

  • [round_temp_variable](/wiki/effect/round_temp_variable):uniform 分布产出的是浮点数,通常需要先取整再传给整数型 effect(如 add_political_power),否则可能产生意外行为。
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable):生成随机值后,用 clamp 将其限定在业务逻辑允许的安全范围内,防止极端值破坏平衡。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable):在随机基础值之上叠加固定偏移量,实现"随机值 + 基础保底"的常见设计模式。
  • [check_variable](/wiki/trigger/check_variable):随机化后通常需要根据结果分支执行不同逻辑,配合 check_variableif 块中判断随机结果区间。

常见坑

  1. 忘记对结果取整uniform 分布默认产出浮点数,若直接将 temp_variable 传给只接受整数的 effect(如 add_manpower),脚本不会报错但会静默截断或产生非预期值,务必先调用 round_temp_variable
  2. binomial 漏写 max / poisson 漏写 lambdabinomial 在指定 minmax 为必填,poissonlambda 始终必填;遗漏这些字段不会有明显错误提示,但分布参数会回退到默认行为,导致随机结果完全不符合预期。

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

randomize_temp_variable is commonly used in event outcomes that require introducing randomness, such as randomizing battle casualties, generating random resource reward amounts, or determining random event branch weights. Compared to hardcoding fixed values, it makes mod results more unpredictable and enhances gameplay experience.

# Randomly generate a political power reward in an event option (uniform distribution between 0~5)
option = {
    name = my_event.option_a
    randomize_temp_variable = {
        var = temp_pp_gain
        distribution = uniform
        min = 0
        max = 5
    }
    round_temp_variable = temp_pp_gain
    add_political_power = temp_pp_gain
}

Synergy

  • [round_temp_variable](/wiki/effect/round_temp_variable): The uniform distribution produces floating-point numbers; you typically need to round first before passing to integer-only effects (such as add_political_power), otherwise unexpected behavior may occur.
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable): After generating a random value, use clamp to constrain it within the safe range permitted by your game logic, preventing extreme values from breaking balance.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): Stack a fixed offset on top of the randomized base value to implement the common "random value + guaranteed minimum" design pattern.
  • [check_variable](/wiki/trigger/check_variable): After randomization, you typically need to branch execution based on the result; pair with check_variable inside if blocks to test which interval the random result falls into.

Common Pitfalls

  1. Forgetting to round the result: The uniform distribution produces floating-point numbers by default. If you pass temp_variable directly to effects that only accept integers (such as add_manpower), the script won't error but will silently truncate or produce unexpected values—always call round_temp_variable first.
  2. Omitting max in binomial / lambda in poisson: binomial requires max to be specified when min is set; poisson always requires lambda. Omitting these fields won't produce obvious error messages, but distribution parameters will revert to default behavior, causing random results to be completely misaligned with your expectations.