Skip to content

HTML UI

Build your HTML, CSS and JavaScript into a static directory, then register it with ui.registerResource. Each registration captures an immutable version. Register during gamemode startup so the launcher can describe its API permissions before players join.

gamemodes/my-mode.ts
import { commands, server, ui } from "nativemp";
import { fileURLToPath } from "node:url";
const menu = await ui.registerResource({
name: "menu",
directory: fileURLToPath(new URL("./menu/dist", import.meta.url)),
apiOrigins: ["https://api.example.com"],
});
commands.register("menu", async player => {
const view = ui.open(player, menu, { interaction: { timeoutMs: 60_000 } });
const result = await view.result;
if (result.status === "submit") server.log(JSON.stringify(result.value));
});

Registration starts no download. ui.preload or the first open downloads missing files and verifies them. Joining never waits for those files. Your gamemode may choose to wait for ui.preload or view.ready before continuing its own login or spawn flow.

Every open creates a separate UiView, even for the same resource and route.

Promise What it means
view.opened A browser view was created, or creation failed.
view.ready The page called window.nativemp.ready() and its first complete frame is available, or readiness failed.
view.result The player submitted/cancelled, a deadline expired, a script closed the view, or its lifetime failed.

A loading view takes no input. Gameplay or the current page remains usable until the new page is ready. It then moves to the top. Only the top interactive view takes keyboard and mouse input; Escape closes it and restores the view below. view.focus() brings a ready view to the top. Set interactive: false for a display that takes no input. Losing game focus releases captured controls.

An interaction enables human responses. Its deadline defaults to 60 seconds from readiness and continues while the view is covered or the game is unfocused. Override it with timeoutMs, from 1 ms to 24 hours. Download and page startup have separate bounded deadlines. Await view.result directly: delivery does not depend on another event handler for that player finishing. The observational UI events still run in order with that player’s other events.

Treat submitted JSON as untrusted. Supply interaction.parse to validate it and return your application’s type; throwing produces failed with invalid-ui-response. A page cannot grant itself money, permissions or other game state: validate every action on the server.

Use packaged script files; inline scripts, evaluated code and remote scripts are blocked. Include server/script/browser.d.ts in your browser TypeScript project’s files for the typed window.nativemp API.

gamemodes/menu/dist/index.html
<!doctype html>
<meta charset="utf-8">
<button id="accept">Accept</button>
<script src="app.js" defer></script>
gamemodes/menu/dist/app.js
const ui = window.nativemp;
ui.onMessage(data => console.log("Server message", data));
document.querySelector("#accept").addEventListener("click", () => {
void ui.submit({ accepted: true });
});
void ui.ready();
Page method Purpose
ready(): Promise<void> Call once the page can display and accept its intended input; resolves after its first complete frame.
emit(name, data?): Promise<void> Send a named message to uiEventReceived. Names start with a letter and use up to 64 ASCII letters, digits, :_.-. The promise rejects with backpressure if the bounded client control queue is full.
submit(data?): Promise<void> Complete an interaction with JSON. Only the top interactive view may submit.
cancel(): Promise<void> Cancel the top interactive view.
onMessage(callback): () => void Receive view.send(data) messages; returns a function that removes this listener.

JSON is limited to 8 KiB encoded as UTF-8 and 16 nested levels. Numbers must be finite. Incoming page messages cannot be changed. view.send returns false until ready or after closing. Browser methods reject invalid or inactive messages; an overloaded event queue rejects only excess emit() calls, keeping other open views alive. Pages have no Node.js, filesystem, shell or general game function access.

Pages use controlled HTTPS origins. Declare exact external HTTPS API origins with apiOrigins; paths, wildcards and credentials are not allowed. Requests and redirects stay within those declarations. Remote navigation and executable assets remain blocked. CORS and HTTPS certificate checks still apply, so your API must permit the page’s origin, for example with an appropriate Access-Control-Allow-Origin response.

HTTP API declarations work only when a player explicitly enables UI development mode in their own development launcher. Production clients disable this option. A server cannot turn it on. Public IP addresses without a domain are supported under the same exact-origin restrictions. HTTP services are unencrypted, including any data the page sends to them; the game’s encrypted connection does not encrypt those separate browser requests.

The launcher warns Unencrypted HTTP UI services before joining a server that declares HTTP capability, including directory, saved and direct connections. It warns even when local development mode is off.

Gamemode reload closes existing views, cancels waits and registers new resource versions. Disconnect clears cookies, local storage and IndexedDB. Verified static files remain cached outside the game installation. Views of the same resource share browser data for their current connection; separate resources use separate data.

The game shows a small top-right spinner for a slow open, without taking input, and a small failure notice if opening fails. Set failureNotice: false when your gamemode supplies its own feedback. uiViewFailed reports the failure. Common codes include resource-timeout, resource-verification-failed, ui-startup-timeout, ui-host-failed, invalid-ui-response, too-many-ui-views and disconnected.

If the UI application fails, existing views fail and controls are released. Your gamemode decides which pages to reopen; a later explicit open can start a recovered application.

Resource limits: 32 registered resources, 256 files per resource, 8 MiB per file, 32 MiB per resource and 128 MiB in total. Paths are relative NFC Unicode, at most 160 UTF-8 bytes and 16 components; links, traversal and filename aliases are rejected. A player may have eight views and four resource downloads in progress.