Wiki

trigger · can_build_railway

Definition

  • Supported scope:any
  • Supported target:any

Description

Checks if a railway can be built according to specifications. Example:
can_build_railway = {
  build_only_on_allied = yes # No by default. If yes and the effect scope is country, it will only build on allied territories for the country

  # You can specify a weight function that will be used in pathing. The scope will be the controller of the province it is trying to path to.
  # A negative value will make it not to path to that controller.
  # Non-negative values will be used as a path cost for that province.
  controller_priority = {
    base = 1

    modifier = {
      tag = MAN
      add = 2
    }
  }

  # The following options are used for picking a path. You can specify multiple options and it will pick in following order:
  fallback = yes # Default no. If yes, each option will try to fallback to next one.
  # option 1: List of provinces to draw railways. If fallback = yes uses start and end provinces of the path as fallback in option 2.
  path = { 10 20 30 40 }
  # option 2: Specify start & end province IDs. It will pick the shortest path. If provinces are not valid and if fallback = yes it will use states of those provs and use in option 3.
  start_province = 42
  target_province = 84
  # option 3: Specify start & end state IDs. It will pick provinces with the best node (capital > nodes > naval )
  start_state = 50
  target_state = 100
}

实战 · 配合 · 坑

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

实战用法

can_build_railway 常用于 mod 中判断是否满足铁路建设条件,例如在决议(decision)的 available 块里限制玩家只有在特定省份之间存在可行路径时才能执行建设操作。也可用在事件选项的 trigger 块里,配合 controller_priority 动态过滤敌方控制区,实现只允许沿友军领土修路的逻辑。

# 决议可用条件示例:仅当首都与目标州之间可建铁路时才开放
available = {
    can_build_railway = {
        build_only_on_allied = yes
        fallback = yes
        start_state = 42
        target_state = 100
        controller_priority = {
            base = 1
            modifier = {
                is_ally_with = ROOT
                add = 3
            }
        }
    }
}

配合关系

  • [build_railway](/wiki/effect/build_railway)can_build_railway 作为前置条件检查,通过后再用 build_railway 实际执行铁路建设,两者参数结构高度对称,可复用相同的 path/start_state/target_state 配置。
  • [has_railway_connection](/wiki/trigger/has_railway_connection):常与本 trigger 组合使用,先用 has_railway_connection 排除已有连接的情况,再用 can_build_railway 确认新路径可行,避免重复建设。
  • [has_railway_level](/wiki/trigger/has_railway_level):用于同时检查目标地区现有铁路等级,结合 can_build_railway 决定是新建还是升级,逻辑更完整。
  • [if](/wiki/effect/if) / [if](/wiki/trigger/if):将 can_build_railway 嵌入 if 条件分支,根据路径是否可行动态选择不同的建设策略或提示文本。

常见坑

  1. pathstart_province/target_provincestart_state/target_state 同时写入时优先级理解错误:三种寻路方式并非等价叠加,游戏按 option 1 → 2 → 3 顺序优先取用,只有开启 fallback = yes 才会在前一选项失败后回退到下一选项;若未开启 fallback,写了多组选项也只会读第一个有效的,后面的会被静默忽略。
  2. controller_priority 返回负值却期望路径仍然通过:负值的设计含义是"拒绝经过该控制者的省份",若误将中立或敌方省份权重设为负数,整条路径可能因无法绕开而判定为不可建,导致 trigger 始终返回 false,排查时容易忽略这一隐性截断。

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

can_build_railway is commonly used in mods to check whether railway construction conditions are met. For example, it can be placed in a decision's available block to restrict players to building only when a viable path exists between specific provinces. It can also be used in event option trigger blocks, combined with controller_priority to dynamically filter enemy-controlled areas and implement logic that only allows building along allied territory.

# Example of decision availability condition: only opens if a railway can be built between capital and target state
available = {
    can_build_railway = {
        build_only_on_allied = yes
        fallback = yes
        start_state = 42
        target_state = 100
        controller_priority = {
            base = 1
            modifier = {
                is_ally_with = ROOT
                add = 3
            }
        }
    }
}

Synergy

  • [build_railway](/wiki/effect/build_railway): can_build_railway serves as a prerequisite check; once passed, use build_railway to actually execute the construction. The two have highly symmetric parameter structures and can reuse the same path/start_state/target_state configuration.
  • [has_railway_connection](/wiki/trigger/has_railway_connection): Commonly combined with this trigger—first use has_railway_connection to exclude cases where a connection already exists, then confirm the new path is viable with can_build_railway to avoid duplicate construction.
  • [has_railway_level](/wiki/trigger/has_railway_level): Used to simultaneously check the existing railway level in the target area; combined with can_build_railway, it determines whether to build new or upgrade existing, making the logic more complete.
  • [if](/wiki/effect/if) / [if](/wiki/trigger/if): Embed can_build_railway within if conditional branches to dynamically select different construction strategies or hint text based on path viability.

Common Pitfalls

  1. Misunderstanding priority when path, start_province/target_province, and start_state/target_state are written simultaneously: The three pathfinding methods are not equivalent stacks. The game processes them in option 1 → 2 → 3 priority order, and only falls back to the next option if the previous one fails when fallback = yes is enabled. Without fallback enabled, even if multiple option sets are written, only the first valid one is read; subsequent ones are silently ignored.
  2. controller_priority returns negative values but you expect the path to still pass: Negative values are designed to mean "refuse to pass through provinces controlled by this entity." If you mistakenly set neutral or enemy province weights to negative, the entire path may be judged impassable due to inability to bypass it, causing the trigger to always return false. This hidden cutoff is easy to overlook during debugging.