Docs/API
Events
Define custom events with register_event in rtv-mp scenarios: options, how chance rolling works, the event lifecycle, how events end, and event handles.
A scenario event is a named function that the server starts in an area by chance on a timer, or on demand with area:start_event(name) or /scenario start. Each event runs at most once per area instance at a time.
Scenario events have no visuals of their own on clients: an event is whatever its function does with the rest of the API (NPCs, messages, native events, items). They run alongside native dynamic events and never replace them.
register_event(name, {
maps = {"Village", "Highway"}, -- public maps it may roll in (default: every public map)
chance = 25, -- percent per roll, 0-100 (default 100)
interval = 900, -- seconds between rolls per area, 10-604800 (default 900)
min_players = 1, -- players needed in the area for a roll to count, 0-64 (default 1)
duration = 600, -- seconds before it ends by itself, 0-604800 (default 0 = see below)
cleanup = function(area, event) end, -- runs once when it ends (optional)
}, function(area, event)
-- the event's main task
end)
The options table is optional: register_event("drill", function(area, event) ... end) uses every default. The name follows the same rules as command names and cannot be a native event key (airdrop, btr_patrol, ...). Each name can be registered once.
Rolling#
- Every loaded public map in
mapsgets its own roll timer for each event. - The first roll is one
intervalafter the area is first seen (when it loads, or when the scenario loads or reloads). Later rolls follow everyinterval. - At each roll the event starts if it is not already running there, the area has at least
min_playersplayers, and a random number from 0 to 99 is belowchance.chance = 0never rolls but can still be started by hand. - Scenario events never start in private hideouts.
Lifecycle#
event_started(area, name, true)hooks fire.- The main function runs as a task with
(area, event)and maywait. - The event ends (see below).
- Its main task is stopped if it is still running. A function that ends its own event stops at its next
wait. event_ended(area, name, reason, true)hooks fire, thencleanup(area, event)runs as its own task.
How a scenario event ends#
| Reason | When |
|---|---|
"finished" | No duration, and the main function returned. |
"error" | No duration, and the main function failed. |
"duration" | duration seconds have passed since it started. With a duration, the event keeps running after its main function returns. |
"stopped" | event:stop(), area:stop_event(name) or /scenario stop. |
"area_unloaded" | Its area was unloaded. |
A reload drops running events without ending them (see reloading).
Event handles#
event:stop()#
Ends the event with reason "stopped". Does nothing if it already ended.
| Field / method | Description |
|---|---|
event.name | The event name. |
event.area | The area handle it runs in. |
event.running | false once it has ended. |
event.started_at | Start time (server.time() seconds). |
event.ends_at | When duration ends it, or nil without a duration. |
Event handles compare with ==, so they work as table keys and owners (see Recipes). Tasks the event function starts with spawn, after or every belong to the scenario, not the event: cancel them in cleanup if they should stop with it.
Found a mistake or missing detail? Tell us in the Discord.
rtv-mp