Wiki

effect · set_temp_variable_to_random

Definition

  • Supported scope:any
  • Supported target:none

Description

sets a temp variable to a random value. example 
set_temp_variable_to_random = num_dogs #sets num_dogs a random value between [0, 1) 
set_temp_variable_to_random = { 
	var = num_dogs #variable to set 
	min = 5 #default 0. value will be set in between [min, max) 
	max = 10 #default 1. value will be set in between [min, max) 
	integer = yes #default no. if yes the number value will be an integer 
}

实战 · 配合 · 坑

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

实战用法

set_temp_variable_to_random 常用于需要引入随机性的事件或决策场景,例如随机化某国的资源产出、随机决定事件伤害数值、或在循环中为每次迭代生成不同的随机权重。以下示例展示了在事件中随机生成一个整数伤害值并扣除人力:

immediate = {
    set_temp_variable_to_random = {
        var = random_casualties
        min = 10
        max = 51
        integer = yes
    }
    add_manpower = {
        value = random_casualties
        multiply = -1
    }
    custom_effect_tooltip = random_casualties_tt
}

配合关系

  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable):随机值产生后,用此命令将结果强制限定在安全区间内,防止极端值导致脚本逻辑出错。
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable):对随机结果进行缩放,例如将 [0,1) 区间的原始值乘以某个系数,得到符合实际需求的量级。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable):在随机值基础上叠加固定偏移量,实现"基础值 + 随机浮动"的典型设计模式。
  • [check_variable](/wiki/trigger/check_variable):在后续 trigger 块中检查已生成的临时变量,根据随机结果决定是否触发分支条件。

常见坑

  1. 混淆简写与块语法的 min/max 默认值:使用简写形式(直接赋变量名)时,范围固定为 [0, 1),如果直接用于整数场景而不加 integer = yes 和自定义区间,几乎永远只会得到 0,需改用完整块语法并显式声明 minmaxinteger
  2. max 值是开区间上界,无法取到:新手常误以为 max = 10 会产生包含 10 的结果,但实际范围是 [min, max),若需要整数结果包含 10,应将 max 设为 11

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

set_temp_variable_to_random is commonly used in event or decision scenarios that require introducing randomness, such as randomizing a country's resource output, randomly determining event damage values, or generating different random weights for each iteration in a loop. The following example demonstrates randomly generating an integer casualty value in an event and deducting manpower:

immediate = {
    set_temp_variable_to_random = {
        var = random_casualties
        min = 10
        max = 51
        integer = yes
    }
    add_manpower = {
        value = random_casualties
        multiply = -1
    }
    custom_effect_tooltip = random_casualties_tt
}

Synergy

  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable): After the random value is generated, use this command to forcibly constrain the result within a safe range, preventing extreme values from breaking script logic.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable): Scale the random result by multiplying it with a coefficient, for example converting a raw value in the [0, 1) interval to a magnitude that meets actual requirements.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): Stack a fixed offset on top of the random value, implementing the typical design pattern of "base value + random fluctuation".
  • [check_variable](/wiki/trigger/check_variable): In subsequent trigger blocks, check the generated temporary variable and decide whether to trigger branch conditions based on the random result.

Common Pitfalls

  1. Confusing shorthand syntax with block syntax min/max defaults: When using shorthand form (directly assigning the variable name), the range is fixed at [0, 1). If used directly in an integer context without adding integer = yes and custom ranges, you will almost always get 0. Use the complete block syntax instead and explicitly declare min, max, and integer.
  2. max value is an open interval upper bound and cannot be reached: Beginners often mistakenly believe that max = 10 will produce results including 10, but the actual range is [min, max). If you need integer results to include 10, set max to 11.