serverConnection - Client-Server Connection Manager
Reference for `serverConnection.lua`. Manages the local client-server connection lifecycle, handling disconnects and the transition from loading to gameplay-ready state.
Reference for serverConnection.lua. Manages the local client-server connection lifecycle, handling disconnects and the transition from loading to gameplay-ready state.
Exports
| Key | Signature | Description |
|---|---|---|
onCameraHandlerSetInitial | () | Called when the first control object is set - transitions world to ready state |
disconnect | (callback?, loadingScreen?) | Disconnects with optional loading screen and callback |
noLoadingScreenDisconnect | (profiler?) | Disconnect without showing loading screen (used during exit) |
M.noLoadingScreenDisconnect | (p) | TODO: clean this up, but not call disconnectActual directly it just will result in the gamestate getting mixed messages |
M.onCameraHandlerSetInitial | () | (No description available) |
M.disconnect | variable | (No description available) |
Internals
onCameraHandlerSetInitial()
Called once when the camera handler is first set after level load:
- Enables cursor-hide-on-inactive for the canvas.
- Sets
worldReadyState = 1(waiting for vehicle materials to finish rendering). - Fires
onWorldReadyStatehook.
The transition from state 1→2 happens in luaPreRender (in main.lua) once vehicle materials are ready or a 5-second timeout elapses.
disconnectActual(callback, loadingScreen, profiler)
Full disconnect sequence:
be:physicsStopSimulation()- halt physics- Re-enable cursor visibility on canvas
- Clear lighting mission variables
server.destroy()- destroy level, physics world, datablocks- Clear mission filename
- Exit loading screen if applicable
- Execute callback if provided
disconnect(callback, loadingScreen)
Wrapper that optionally enters a loading screen before disconnecting. Uses core_gamestate.requestEnterLoadingScreen for managed transition.
How It Works
BeamNG uses a local client-server architecture even in single-player. This module manages that connection's lifecycle. The key transition point is onCameraHandlerSetInitial, which signals that the level is loaded enough for the camera/player to become active. Disconnect tears down everything via the server module.
-- Disconnect and return to menu
serverConnection.disconnect(function()
core_gamestate.requestGameState()
end)
-- Quick disconnect during shutdown (no loading screen)
serverConnection.noLoadingScreenDisconnect()server/server - Level Loading & Game Server
Reference for `server/server.lua`. Manages level loading, mission lifecycle, physics initialization, and the loading screen. Despite the name, this is a local "server" - BeamNG runs a client-server ar
`setSpawnpoint` - Default Spawn Point Persistence
Manages per-level default spawn point selection. Saves and loads the player's chosen spawn point to/from `settings/cloud/game-state.json`.