Train/Rail Save Load
Overview
Use this skill for train/rail persistence changes under the current runtime architecture.
Reuse-First Rule
- Before adding save/load helpers, search existing train/rail persistence code first.
- Prefer existing
Game.Trainsave/load components over parallel implementations. - If duplication is unavoidable, record
WHY_NEW_IMPLEMENTATIONin code/PR notes. - Recommended pre-check:
rg --line-number "SaveLoad|Restore|ConnectionDestination|RailPosition|TrainUnitSaveData" moorestech_server/Assets/Scripts
Current Architecture (Must Preserve)
- Rail connectivity persists as
railSegmentsand restores viaRailGraphSaveLoadService. - Train state persists as
trainUnitsand restores viaTrainSaveLoadService. - Rail block placement/load and segment connectivity restoration are separate steps.
Snapshot Boundary Contract (Must Preserve)
- Runtime train sync uses:
- per-unit snapshot event (
va:event:trainUnitSnapshot) - full snapshot API (
va:getTrainUnitSnapshots)
- per-unit snapshot event (
- If TrainUnit/TrainCar state shape changes, update save/load and snapshot serialization together.
- Do not persist transport-only runtime values (sequence IDs, future buffers, queue internals).
Save Data Model
railSegments(RailSegmentSaveData) contains:A/B(ConnectionDestination)LengthRailTypeGuidIsDrawable
trainUnits(TrainUnitSaveData) contains rail position snapshot + train state + cars + diagram.TrainCarSaveDataincludesTrainCarMasterId,IsFacingForward, and stateful car data.
Load Order Contract (Must Preserve)
- Load world blocks.
- Restore rail segments.
- Restore common world/player/entity state.
- Restore train units.
- Restore docking links.
Reason: train rail-position restoration resolves ConnectionDestination via rail graph provider; restore order must keep this mapping valid.
Rail Segment Restore Rules
- Skip null segment entries safely.
- For drawable segments, keep current length validation/correction behavior.
- Restore through datastore APIs (
TryRestoreRailSegment), not ad-hoc graph mutation.
Train Restore Rules
- Reset previously registered runtime train state before restore.
- Resolve rail position through provider/factory from saved snapshot data.
- Restore TrainUnit and TrainCar as one consistent unit.
- Rebuild docking state after unit restore pass.
- Fail safely per unit on invalid references; avoid corrupting global restore state.
Implementation Checklist
- Update serializer and restore path together for new fields.
- Update snapshot serialization/apply when state affects runtime sync.
- Keep
railSegmentsandtrainUnitscompatible with load order. - Keep
ConnectionDestinationresolution contract valid. - Add regression tests for rail reconstruction and train restore consistency.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.