Wiki

trigger · 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 作为 effect 实际上常被用于在 if 条件触发器前先生成随机数、再用 check_variable 做概率判断,适合实现自定义的随机事件概率系统、随机奖惩数值或程序化地图生成逻辑。例如在 mod 中模拟随机补给量或随机科技加成时,先生成随机值再在触发器中比较:

# effect 块中先赋值
set_temp_variable_to_random = {
    var = roll_value
    min = 0
    max = 100
    integer = yes
}
# 随后在 trigger/if 中判断
if = {
    limit = {
        check_variable = { roll_value < 30 }
    }
    # 低概率分支逻辑
}

配合关系

  • [check_variable](/wiki/trigger/check_variable):生成随机临时变量后,几乎必须配合 check_variable 来比较随机值是否落在目标区间,从而实现概率门控。
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable):在随机值基础上叠加固定偏移量(如难度加成),再做后续判断或赋值。
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable):随机结果生成后用 clamp_temp_variable 将其限定在安全范围内,防止极端值破坏游戏平衡。
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable):对随机值做缩放(如乘以某国的工业系数),将原始随机数转换为有业务意义的最终数值。

常见坑

  1. 将其当 trigger 使用set_temp_variable_to_random 本质是 effect,不能直接写在纯 trigger 块(如 limit = { ... })内部;正确做法是在外层 effect 块中先执行赋值,再进入带 limitif 分支做判断。
  2. 忽略区间左闭右开特性:随机范围是 [min, max),若需要恰好包含 max 值(如骰点 1-6 含 6),应将 max 设为目标最大值加 1,否则最大值永远无法被取到。

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 as an effect is commonly used to generate a random number before an if conditional trigger, then validate probability using check_variable. This pattern is ideal for implementing custom random event probability systems, randomized reward/penalty values, or procedural map generation logic. For example, when simulating random supply amounts or randomized technology bonuses in a mod, generate the random value first, then compare it in a trigger:

# Assign value in effect block first
set_temp_variable_to_random = {
    var = roll_value
    min = 0
    max = 100
    integer = yes
}
# Then check in trigger/if
if = {
    limit = {
        check_variable = { roll_value < 30 }
    }
    # Low probability branch logic
}

Synergy

  • [check_variable](/wiki/trigger/check_variable): After generating a random temporary variable, you almost always need to pair it with check_variable to compare whether the random value falls within a target range, thus implementing probability gating.
  • [add_to_temp_variable](/wiki/effect/add_to_temp_variable): Add a fixed offset to the random value (such as difficulty bonuses), then perform subsequent checks or assignments.
  • [clamp_temp_variable](/wiki/effect/clamp_temp_variable): After generating the random result, use clamp_temp_variable to constrain it within a safe range, preventing extreme values from breaking game balance.
  • [multiply_temp_variable](/wiki/effect/multiply_temp_variable): Scale the random value (such as multiplying by a country's industrial coefficient) to convert the raw random number into a meaningful final value.

Common Pitfalls

  1. Using it as a trigger: set_temp_variable_to_random is fundamentally an effect and cannot be written directly inside a pure trigger block (such as limit = { ... }). The correct approach is to execute the assignment in an outer effect block first, then enter an if branch with limit to perform the check.
  2. Overlooking the half-open interval behavior: The random range is [min, max). If you need to include the max value exactly (like a dice roll 1-6 including 6), set max to the target maximum plus 1; otherwise the maximum value can never be rolled.