HTML interfaces
SixM's “NUI”: HTML/CSS/JS pages inside the game, the /__sixm.js bridge, focus and Esc.
Each resource can have an HTML page that is drawn inside the game, on top of the image, even full screen. It is what FiveM calls NUI.
Declaring it
-- recurso.lua
return {
cliente = { "cliente.lua" },
ui = "ui/index.html",
archivos = { "ui/*" }, -- its CSS, JS, images and fonts
}
The page
Normal HTML, CSS and JS. To talk to the resource's client side (Lua or JS), load the bridge:
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="estilo.css" />
</head>
<body>
<div id="caja" hidden>
<h1 id="titulo"></h1>
<button id="comprar">Comprar</button>
<button id="cerrar">Cerrar</button>
</div>
<script src="/__sixm.js"></script>
<script>
const caja = document.getElementById('caja');
sixm.on('abrir', (datos) => { // what sixm.ui.enviar('abrir', datos) sends
document.getElementById('titulo').textContent = datos.titulo;
caja.hidden = false;
});
sixm.on('cerrar', () => { caja.hidden = true; });
document.getElementById('comprar').onclick = () => sixm.enviar('comprar', { articulo: 3 });
document.getElementById('cerrar').onclick = () => { caja.hidden = true; sixm.cerrar(); };
</script>
</body>
</html>
| In the page | What it does |
|---|---|
sixm.on(tipo, fn) | receives what sixm.ui.enviar(tipo, datos) sends from the client |
sixm.enviar(tipo, datos) | arrives at sixm.ui.on(tipo, fn) on the client |
sixm.cerrar() | gives control back to the game (like Esc) |
The client side
-- cliente.lua
sixm.tecla("K", function()
sixm.ui.enviar("abrir", { titulo = "Tienda" })
sixm.ui.foco(true) -- cursor and keyboard for the page
end)
sixm.ui.on("comprar", function(d)
sixm.al_servidor("mitienda:comprar", d.articulo)
end)
-- Esc with the page in front arrives here
sixm.ui.on("escape", function()
sixm.ui.enviar("cerrar")
sixm.ui.foco(false)
end)
How it looks
- Each interface fills the whole screen: a 1920x1080 canvas scaled to the game's resolution, with a transparent background. Draw only your own content.
- With
sixm.ui.foco(true), the player has a cursor and the game receives neither mouse nor keyboard. Withfalse, control returns to the game (the page stays visible: useful for a HUD). - Several interfaces can be visible at once (chat, notices, roleplay HUD...). Esc goes to the one that has focus.
Limits
- Each page has its own origin,
sixm-ui://<resource>/: it can uselocalStoragewithout clashing with other resources. - No network: it loads nothing from the Internet, neither fonts nor libraries from a CDN. Everything it uses goes inside the resource, in
archivos. - All of a resource's client content (code and interface) fits in 8 MB.
- Recommended in the CSS:
[hidden] { display: none !important; }, so thehiddenattribute works even if the element has adisplay.
Loading screen
A server's loading screen is also a page: a resource's folder with its index.html, set with pantalla_carga <resource> in servidor.cfg. The launcher downloads it over TCP and shows it from the moment you press Entrar (Join) until a resource calls sixm.carga_fin() (the bundled aparecer does it, once the player is on the ground).