Vehicle Boot Sequence & Lifecycle
The exact order in which vehicle systems initialize — from JBeam compilation to physics loops. Essential for timing your VE code correctly.
Understanding the order in which the vehicle environment boots is critical for ensuring your code runs at the right time. Getting the timing wrong means your extension tries to read data that doesn't exist yet, or misses the window to configure systems before they start running.
:::tip[Quick Rule]
Use onInit for setup, onVehicleLoaded for anything that needs the physical vehicle, and updateGFX for per-frame logic.
:::
1. The Boot Sequence (Startup)
When a vehicle is spawned or the Lua VM is reloaded:
- Environment Setup:
main.luasets up global constants (vmType,package.path). - Core Initialization:
init()inmain.luais called.- Raw JBeam is loaded and compiled into
v.data. - Core modules (
electrics,powertrain,wheels,beamstate) are required.
- Raw JBeam is loaded and compiled into
initSystems():- Stage 1:
electrics.init(),damageTracker.init(),beamstate.init(). - Stage 2:
powertrain.init(),controller.init(). - Stage 3:
sounds.init(),props.init(),fire.init().
- Stage 1:
- Extension Hooks:
extensions.hook("onInit")is called for all loaded extensions. - Post-Spawn:
extensions.hook("onVehicleLoaded")is called. The vehicle is now physically in the world.
2. The Frame Loop (Runtime)
Every frame (Graphics Step):
sensors.updateGFX()input.updateGFX()electrics.updateGFX()extensions.hook("updateGFX")-- Your extension logic usually goes here.powertrain.updateGFX()guihooks.sendStreams()-- Data sent to UI.props.updateGFX()-- Needle/prop movement.
3. The Physics Loop (High-Frequency)
Only runs if a module requests it via obj:setPhysicsStepEnabled(true). Runs at 2000Hz.
powertrain.update()controller.update()extensions.hook("onPhysicsStep")
4. Reset Sequence (Pressing 'R')
When the user resets the vehicle:
onVehicleReset()is called inmain.lua.guihooks.reset().extensions.hook("onReset").- Every core module's
.reset()function is called (e.g.,electrics.reset(),powertrain.reset()).
Key Hooks for Developers
| Hook | Timing | Purpose |
|---|---|---|
onInit | During boot | Setup variables, load dependent modules. |
onVehicleLoaded | After physics init | Start logic that requires a physical object (like timers). |
updateGFX(dt) | Every frame | Main logic, UI updates, visual changes. |
onPhysicsStep(dt) | 2000Hz | Low-level torque/force/actuator control. |
onReset | During reset | Clean up state, stop active timers/modes. |
onDespawnObject | Before removal | Final cleanup, saving data to GE. |
Inter-VM Communication (GE ↔ VE ↔ UI)
Complete guide to cross-VM communication — GE to VE commands, VE to GE callbacks, peer vehicle messaging, UI events, and the electrics data bus.
Lua Classes & Metatables
When and how to use Lua classes in BeamNG modding — metatables, constructors, inheritance, with real examples like parcels, checkpoints, and NPC fleets.