Docs/Getting started
Getting started
Write your first scenario: the folder layout, how scenarios are loaded and reloaded, and a working example.
These pages describe the scenario system of the rtv-mp dedicated server. They are published early so you can see what is possible. The API may still change, and hosting your own server is not available yet.
Scenarios are data files and Lua scripts that add rules to an rtv-mp server. Balance lives in data files such as loot.json; scripts are optional and add custom logic. This document covers every file a scenario can contain, everything a script can call, when its code runs, and what it is not allowed to do.
API version: 1 (server.api). The server runs gopher-lua, which implements Lua 5.1.
Quick start#
- Copy
scenarios/exampletoscenarios/myserver. Thescenariosfolder sits next toserver-settings.json. - Set
"scenario": "myserver"inserver-settings.jsonand start the server. The log shows[scenario] loaded scenario=myserver .... - Edit
scenarios/myserver/init.luaand type/scenario reloadin the server console, or in chat as an admin.
on("player_spawn", function(player, reason)
if reason == "join" then
player:tell("Welcome, " .. player.name)
end
end)
command("online", function(player, args)
player:tell(#server.players() .. " players online")
end)
register_event("ambush", {
maps = {"Village"},
chance = 20,
duration = 600,
}, function(area, event)
for i = 1, 3 do
local position = area:random_position()
if position then
area:spawn_npc(position, "bandit")
end
end
area:tell("You hear voices in the woods...")
end)
Folder layout and loading#
server-settings.json "scenario": "myserver" (empty = the scenario built into the binary)
scenarios/
default/ readable copy of the built-in scenario
myserver/
scenario.json optional: name, base, maps, start_area
globals.json, world.json, optional data files
players.json, events.json,
ai.json, loot.json,
presets.json, economy.json
zones/<Map>.json optional zone data
events/positions/<Map>.json optional event positions
init.lua optional script entry point
lib/messages.lua loaded with require("lib.messages")
data/scenarios/myserver.json saved data written by store (not edited by hand)
Every file is optional, but the folder must exist. A scenario can be data only or scripts only. Data files and their layering are documented in docs/; this page covers the Lua side. The default scenario uses vanilla-style loot rolls; economy.json can opt into a count-based restock system.
- Selecting a scenario. The
scenariosetting is a folder name: 1 to 64 letters, digits,_or-. An empty or missing setting runs the built-in default scenario, which has no Lua; the log says[scenario] built-in default. The server looks for the folder next to the settings file passed with-settings. - Base chain.
scenario.jsonbasenames the scenario to start from (default: the built-in default, always the copy inside the binary). Only the selected scenario'sinit.luaruns, never a base's.requirelooks in the selected folder first, then in each base folder on disk. - Startup. The server reads all data files and the saved data, then runs
init.luaonce, before it opens. If any of these fail (an invalid value, unreadable saved data, a script error, or more than 2 seconds of script work), the server does not start, as with an invalid setting. - What
init.luashould do. Register hooks, commands and events, and set up module state. Areas are not loaded yet while it runs at startup; use thearea_loadedhook for work that needs an area. - Reloading.
/scenario reloadreads every data file again, builds a new Lua state and runsinit.luaagain. If anything fails, the error is reported and the old scenario (data and scripts) keeps running unchanged. If it succeeds:- The new data applies from then on: loaded areas get the new
globals.jsonrules at once, and new loot rolls, spawns, events and loadouts use the new files. Loot already in the world is not rerolled; use/respawnlootfor that. - A changed
base,maps,start_areaorapi_versioninscenario.jsonis refused (restart the server); so is a changedworld.jsonseason.force(it is applied only when the world loads). - Saved data (
store) is kept as it is. - The old state is dropped, along with all its waiting tasks, timers, running scenario events and module variables. Those events end without
event_endedorcleanup. NPCs they spawned stay in the world. - Players and areas already online are recorded silently. No
player_join,player_spawnorarea_loadedfires for them again; useserver.players()andserver.areas()ininit.luaif you need them. - Every scenario event's first roll is one interval after the reload.
- The
scenariosetting itself is read only at startup. Switching to a different scenario folder needs a restart.
- The new data applies from then on: loaded areas get the new
Reference
Lua runtime
API
Guides
JSON
Found a mistake or missing detail? Tell us in the Discord.
rtv-mp