SixM
Single player ↗

JavaScript

The same API in QuickJS: await to wait, promises when creating entities, BigInt for models and what is missing.

A resource can be written entirely in JavaScript (or mix .js and .lua files). It runs in QuickJS, on the server and in the game, with the same API as Lua: sixm.on, sixm.al_cliente, sixm.kvp, nat.NAME... The bundled resource anuncios (announcements) is written entirely in JS as an example.

// servidor.js
const rp = sixm.importa('sixm-rp');                 // it doesn't matter that sixm-rp is in Lua
sixm.red('taller:reparar');
sixm.on('taller:reparar', async (id, precio) => {
  if (!rp.quitar_dinero(id, precio, 'efectivo', 'taller')) return;
  await sixm.esperar(3000);                          // waits are done with await
  sixm.al_cliente(id, 'taller:listo');
});
sixm.comando('taller', (id, args) => console.log(`taller pedido por ${id}: ${args.join(' ')}`));
// cliente.js
sixm.tecla('E', async () => {
  const yo = nat.PLAYER_PED_ID();                    // entity: { id, lo, hi }
  const p = nat.GET_ENTITY_COORDS(yo, true);         // { x, y, z }
  const mecanico = await sixm.crear_peaton(sixm.modelo_jugador(), { x: p.x + 2, y: p.y, z: p.z }, 0);
  if (mecanico) sixm.tras(10000, () => sixm.borrar(mecanico));
});

What differs from Lua

LuaJS
Waitingsixm.esperar(ms)await sixm.esperar(ms) inside an async function
Creating entitiescallback fn(ent)returns a promise with the entity (or null); the callback also works
Entityopaque value, compared with ==frozen object { id, lo, hi }; same entity = same id
64-bit modelsintegerBigInt (0x...n); natives accept a number or a BigInt
Native outputsmultiple resultslist [result, output1, ...]
Export with several valuesall of themsixm.importa(r).f(...) returns the first one
Timerssixm.tras, sixm.cadathe same, plus setTimeout / setInterval
Loggingprintconsole.log and print
sixm.peaton_cercanoyesno

Rules

  • An export cannot be async: it returns the value directly.
  • Configuration shared between files uses var (var Config = {...} in config.js): const and let are not visible from the other files.
  • No require, import, fetch, files or network. Each resource is a separate context, with up to 256 MB.
  • A single step cannot exceed 2 s: it is cut off with interrupted and cannot be caught with try.

Full example

The anuncios resource (a JS server side that uses exports from two Lua resources):

// config.js
var Config = { consejos_cada: 10, consejos: ['Pulsa T para hablar.'], duracion: 8, historial: 20 };
// servidor.js
const base = sixm.importa('sixm-base');
const rp = sixm.importa('sixm-rp');
const historial = () => sixm.kvp.get('historial') ?? [];

function anunciar(de, texto) {
  const a = { de, texto, cuando: Date.now(), duracion: Config.duracion };
  sixm.kvp.set('historial', [a, ...historial()].slice(0, Config.historial));
  sixm.al_cliente(-1, 'anuncios:mostrar', a);
}

sixm.comando('anuncio', (id, args) => {
  if (!rp.es_admin(id)) return base.avisar(id, 'Solo los admins pueden anunciar.', 'error');
  const texto = args.join(' ').trim().slice(0, 200);
  if (!texto) return base.decir(id, 'Uso: /anuncio <texto>');
  anunciar(base.nombre(id), texto);
});

sixm.on('jugador:entra', async (id) => {
  const [ultimo] = historial();
  if (!ultimo || Date.now() - ultimo.cuando > 10 * 60 * 1000) return;
  await sixm.esperar(5000);
  sixm.al_cliente(id, 'anuncios:mostrar', ultimo);
});