Common API
What exists on both server and client, in Lua and JS: events, threads, waits, timers and logging.
All of this exists identically on the server side and the client side, and in Lua and in JavaScript.
| Call | What it does |
|---|---|
sixm.recurso | this resource's name |
sixm.on(evento, fn) | fn(...) every time the event arrives; sixm.esperar can be used inside |
sixm.emitir(evento, ...) | event to the resources on the same side (server → server, client → client) |
sixm.hilo(fn, ...) | runs fn in its own thread |
sixm.esperar(ms) | pauses the current thread for ms milliseconds (0 = until the next step) |
sixm.tras(ms, fn) | calls fn once, after ms |
sixm.cada(ms, fn) | calls fn every ms; returns an object with parar() (stop) |
print(...), sixm.log(...) | to the log, with the resource's name |
To communicate between server and client: Events and networking.
Threads and waits
sixm.esperar only works inside a thread, an event handler or a timer:
sixm.hilo(function()
while true do
print("sigo aquí")
sixm.esperar(5000)
end
end)
local t = sixm.cada(1000, function() print("tic") end)
sixm.tras(10000, function() t:parar() end)
In JS, sixm.esperar returns a promise and is used with await:
sixm.hilo(async () => {
for (;;) {
console.log('sigo aquí');
await sixm.esperar(5000);
}
});
const t = sixm.cada(1000, () => console.log('tic'));
sixm.tras(10000, () => t.parar());
Time limit
A single script step (a handler, a timer, a file at startup, an export) cannot exceed 2 seconds. If it does, it is cut off with sixm: tiempo agotado (in JS, interrupted), it is written to the log and everything else carries on. It cannot be caught with pcall or try.
For long jobs, split them up with sixm.esperar(0):
sixm.hilo(function()
for i = 1, #lista do
procesar(lista[i])
if i % 100 == 0 then sixm.esperar(0) end
end
end)
System events
| Event | Side | Arguments |
|---|---|---|
recurso:arranca | both | nombre of the resource that is starting |
recurso:para | both | nombre of the resource that is stopping (it arrives for any resource: compare nombre == sixm.recurso) |
jugador:entra | server | id |
jugador:sale | server | id, motivo (reason) |
sixm.on("recurso:para", function(nombre)
if nombre ~= sixm.recurso then return end
-- save whatever is pending
end)
What can be sent
The arguments of events, exports and storage can be: nil, booleans, numbers (integers or decimals), strings and tables (lists and dictionaries, nested up to 32 levels). In JS, objects and arrays. Functions and entities do not travel over the network.