Wiki

trigger · print_variables

Definition

  • Supported scope:any
  • Supported target:none

Description

prints all variables in scope and temp variables to a file
Example: print_variables = {
file = log_file
text = header_text
append = yes
print_global = yes
var_list = { a b c } #optional
}

实战 · 配合 · 坑

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

实战用法

print_variables 主要用于 mod 调试阶段,当你需要追踪某个 scope 下大量变量的实际值时(例如经济系统、科研点数或权力平衡数值是否按预期赋值),可以在 event 或 focus 的 trigger 块中临时插入它,将结果输出到日志文件方便排查。常见场景是在复杂的变量运算链条(如循环累加后)验证中间结果,省去逐条 log 的麻烦。

# 在 event 的 trigger 块中临时调试:输出当前国家 scope 下所有变量
print_variables = {
    file = "debug_output"
    text = "=== Economic Variables Check ==="
    append = yes
    print_global = no
    var_list = { economy_score trade_value manpower_pool }
}

配合关系

  • [log](/wiki/trigger/log)log 可以在同一调试流程中输出固定文本标记(如当前 scope tag、日期),与 print_variables 的变量快照配合,让日志文件上下文更清晰。
  • [set_temp_variable](/wiki/effect/set_temp_variable):在 effect 块中用 set_temp_variable 做中间运算后,立即用 print_variables 确认临时变量值是否写入正确。
  • [check_variable](/wiki/trigger/check_variable):正式逻辑上线后用 check_variable 替代调试用的 print_variables,两者在开发阶段形成"先打印确认值、再写条件判断"的工作流。
  • [if](/wiki/effect/if):在 if 的条件分支内插入 print_variables,可以只在特定条件满足时才输出变量,避免日志被无关信息淹没。

常见坑

  1. 忘记在发布版本中移除print_variables 是纯调试工具,每次触发都会写入文件,若遗留在正式 mod 中会持续产生 IO 开销并污染玩家日志,务必在上线前删除或用 [is_debug](/wiki/trigger/is_debug) 包裹限制其仅在调试模式下执行。
  2. 误以为它在 effect 块中无效:该命令虽然归类为 trigger(返回值始终为真),但实际使用时常被放入 effect 上下文中调试,新手容易因为"trigger 不能放进 effect 块"的印象而不敢使用,实际上 HOI4 的许多调试 trigger 在 effect 块中同样可以执行,关键是理解它的副作用(写文件)才是调用目的。

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

print_variables is primarily used during mod debugging. When you need to track the actual values of numerous variables within a given scope (for example, whether economic systems, research points, or power balance values are assigned as expected), you can temporarily insert it into the trigger block of an event or focus, and output the results to a log file for convenient inspection. A common scenario is verifying intermediate results after complex variable computation chains (such as accumulated loops), saving the trouble of using individual log commands for each variable.

# Temporary debugging in event trigger block: output all variables under current country scope
print_variables = {
    file = "debug_output"
    text = "=== Economic Variables Check ==="
    append = yes
    print_global = no
    var_list = { economy_score trade_value manpower_pool }
}

Synergy

  • [log](/wiki/trigger/log): log can output fixed text markers (such as current scope tag, date) in the same debugging workflow, and when paired with print_variables variable snapshots, it makes the context in the log file much clearer.
  • [set_temp_variable](/wiki/effect/set_temp_variable): After performing intermediate calculations using set_temp_variable in an effect block, immediately use print_variables to confirm whether the temporary variable values are written correctly.
  • [check_variable](/wiki/trigger/check_variable): Once your formal logic goes live, replace the debugging print_variables with check_variable; the two form a "print to confirm values first, then write conditional checks" workflow during development.
  • [if](/wiki/effect/if): Insert print_variables inside the conditional branches of if, so variables are only output when specific conditions are met, avoiding the log from being flooded with irrelevant information.

Common Pitfalls

  1. Forgetting to remove it from the release version: print_variables is a pure debugging tool that writes to a file every time it triggers. If left in the official mod, it will continuously incur IO overhead and pollute player logs. You must delete it or wrap it with [is_debug](/wiki/trigger/is_debug) before release to restrict execution to debug mode only.
  2. Mistakenly believing it is ineffective in effect blocks: Although this command is classified as a trigger (its return value is always true), in practice it is often placed in effect contexts for debugging. Beginners hesitate to use it due to the impression that "triggers cannot be placed in effect blocks," but in reality, many HOI4 debugging triggers can execute in effect blocks as well. The key is understanding that its side effect (writing to file) is the purpose of the call.