Knowledge from the fieldRedM

Routing an AI train through MULTIPLE forks: the global all-junctions toggle fails — clear all, then set each specific (trackHash, junctionIndex) per leg. Single-junction control DOES work

learning:40

Routing an AI train through MULTIPLE forks: the global all-junctions toggle fails — clear all, then set each specific (trackHash, junctionIndex) per leg. Single-junction control DOES work

Context

You have a self-driving (AI / ghost) mission train that must travel a multi-station route, passing several track forks on each leg. You need it to pick the correct branch at every fork.

The trap

A common pattern (and a prior finding) is to brute-force SetTrainTrackJunctionSwitch over ALL junction indices to the same boolean — "all switches on" / "all switches off." That works for a PLAYER flipping a single fork they're approaching, but it CANNOT route an AI train across multiple forks on one leg: "all on" might send it left at fork A (correct) but also left at fork B (wrong). The global toggle can't express different choices per fork. Result: the AI train ends up at the wrong station.

Also note a misleading earlier impression: that setting ONE specific junction "does nothing." That was a WRONG-INDEX error, not a broken API.

The fix (verified — AI train drove Rhodes→Blackwater correctly through several forks)

Per-fork control works. For each leg: clear all junctions, then set ONLY the specific junctions that leg needs, each to the branch that keeps the train on the path to the destination.

SetAllJunctionsCleared()                    -- reset to default
-- re-assert any auto/broken junctions here (they get cleared too)
for _, fork in ipairs(legForks) do
    -- fork.track = e.g. 'FREIGHT_GROUP', fork.junc = index, fork.state = bool
    SetTrainTrackJunctionSwitch(joaat(fork.track), fork.junc, fork.state)
end

Identifying each fork's (track, junctionIndex):

  • These come straight from a track-script config's track_group + junction_index (rsd_railroad's config_switch.lua is a readable reference). track_group maps to a track-config name (FREIGHT_GROUP, TRAINS3, SPOONI01..04, etc.); junction_index is the literal index.
  • VERIFIED examples on a spooni-tracks map: Valentine/Rhodes = FREIGHT_GROUP junc 2; the Blackwater-bound western leg needed FREIGHT_GROUP junc 3 & 4 + SPOONI junctions all set toward Blackwater.

Whoever OWNS the train (the simulating client) must set these junctions — the train routes off the owner's local junction state. For an AI train, that's the host client. For a player train, the driver. Junction state is client-local and not auto-synced across clients.


Tags: trains, vehicles, track-junction, ai, routing, railroad, switch
Category: natives
Source: scandi-railroad-poc
Created: 2026-05-22T19:25:35.260Z

Back to documentation