effect · randomize_variable
Definition
- Supported scope:
any - Supported target:
none
Description
refer to randomize_temp_variable
randomize_variableanynonerefer to randomize_temp_variable
实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。
randomize_variable 的实际行为等同于 randomize_temp_variable,用于对一个已声明的普通变量(非临时变量)赋予随机数值,常见于需要持久化随机结果的场景,例如给某个国家生成一次性随机事件权重或随机资源储量。与临时变量版本的区别在于,结果会保存在 scope 上供后续逻辑反复读取。
# 为当前国家生成一个随机储量值并存入持久变量
country_event = {
immediate = {
set_variable = { var = resource_roll value = 0 }
randomize_variable = {
var = resource_roll
min = 1
max = 100
}
if = {
limit = { check_variable = { var = resource_roll value > 80 } }
add_victory_points = { province = 1234 value = 5 }
}
}
}
[set_variable](/wiki/effect/set_variable):在调用 randomize_variable 之前先用 set_variable 将变量初始化为 0,避免读取未定义变量导致脚本报错。[check_variable](/wiki/trigger/check_variable):随机化完成后,用 check_variable 对结果区间进行判断,从而分支执行不同的游戏逻辑。[clamp_variable](/wiki/effect/clamp_variable):随机值生成后可用 clamp_variable 将其强制约束在安全范围内,防止边界溢出影响后续计算。[randomize_temp_variable](/wiki/effect/randomize_temp_variable):若随机结果只在当前 effect 块内使用一次,改用 randomize_temp_variable 性能更优,两者语法完全一致,按需选择。set_variable 过的变量调用 randomize_variable,在部分版本中会产生脚本警告甚至静默失败,务必先用 set_variable 赋初值。randomize_variable 操作的是持久变量,若变量名以 temp_ 命名约定书写但实为普通变量,逻辑上不会出错,但容易与 randomize_temp_variable 混淆,导致跨 effect 块读取时拿到的是未初始化的临时变量而非预期的持久值。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.
randomize_variable behaves identically to randomize_temp_variable and is used to assign a random numerical value to an already-declared persistent variable (not a temporary one). It is commonly used in scenarios requiring the random result to persist, such as generating a one-time random event weight or random resource stockpile for a country. The key difference from the temporary variable version is that the result is saved on the scope for subsequent logic to read repeatedly.
# Generate a random stockpile value for the current country and store it in a persistent variable
country_event = {
immediate = {
set_variable = { var = resource_roll value = 0 }
randomize_variable = {
var = resource_roll
min = 1
max = 100
}
if = {
limit = { check_variable = { var = resource_roll value > 80 } }
add_victory_points = { province = 1234 value = 5 }
}
}
}
[set_variable](/wiki/effect/set_variable): Initialize the variable to 0 using set_variable before calling randomize_variable to avoid script errors from reading undefined variables.[check_variable](/wiki/trigger/check_variable): After randomization completes, use check_variable to evaluate the result range and branch into different game logic accordingly.[clamp_variable](/wiki/effect/clamp_variable): After the random value is generated, use clamp_variable to forcefully constrain it within a safe range, preventing boundary overflow from affecting subsequent calculations.[randomize_temp_variable](/wiki/effect/randomize_temp_variable): If the random result is only used once within the current effect block, use randomize_temp_variable instead for better performance. The syntax is identical; choose based on your needs.randomize_variable directly on a variable that was never set with set_variable may produce script warnings or silent failures in some versions. Always initialize with set_variable first.randomize_variable operates on persistent variables. If a variable is named with the temp_ convention but is actually a persistent variable, there will be no logical error, but it can easily be confused with randomize_temp_variable, causing reads across effect blocks to retrieve an uninitialized temporary variable instead of the expected persistent value.