HTML interface

Usage guide of ui.webui: the page API, transports, input sharing between the page and the game, settings and defaults, building the page, fragments of presentation modules, events and service, engines and limits.

Source: module-kits:kits/ui_webui/usage/en.mdView Markdown source

Also read offline — CLI: grip-forge docs ui-webuiMCP tool: gripforge_docs { "topic": "ui-webui" }

HTML interface over your game (ui.webui) #

Draw menus, HUD, inventories and dialogs as a web page over the game, written once, with one page API, on every output. Readable offline: gripforge docs ui-webui, or the MCP tool gripforge_docs (topic ui-webui).

What it is for #

ui.webui is the bridge between an HTML / JavaScript page and the game. The page can use any framework (or plain DOM) and is built by a web bundler. The kit pushes the game state to the page, announces the commands the page sends, and shares the pointer and the keyboard between the page and the game. It decides nothing of the game: a command of the page is announced (ui.command) and the kit or rule that answers it owns the decision.

OutputHow the page is drawn
Web player, Godot web exportDOM loaded into the play document, above the game canvas
Godot, Unity, Unreal desktopthe ui.webui presentation module: the system web view over the game (Windows, macOS) or an embedded browser writing into a GPU texture (Linux); see "Desktop, mobile and web"
Native playerthe HUD and menus are always ui.webui (fragments and ui.tree), never a second UI

Turn it on #

gripforge_gamekit_install   { project_id: "gkp_…", id: "ui.webui" }
gripforge_gamekit_configure { project_id: "gkp_…", id: "ui.webui", config: { "stateHz": 15, "forwardEvents": ["score.changed"] } }

It requires game.engine. On Godot, Unity and Unreal the game chooses between the engine's native UI and ui.webui; by default one way per game (mixNativeUi: true keeps both, best avoided). The full reference is gripforge kit api ui.webui.

The page #

gf-webui.js (no framework, no dependency) is the same file on every output:

import { createBridge } from './gf-webui.js'

const bridge = createBridge({
  onState: (state) => render(state),           // full state, pushed by the game when it changes
  onEvent: (kind, data) => toast(kind, data),  // one-shot events
  onOpen: () => bridge.request(),              // (re)connected: ask for the full state again
})
bridge.start()
button.onclick = () => bridge.send('heal', { amount: 10 })

The page picks its transport by itself: web (the host defined window.__gfui_send), ipc (injected by the engine into the off-screen page), devws (a WebSocket to the game, the page open in a normal browser) or mock (?mock=1, no game).

Two layout rules make the input sharing exact:

  1. a transparent background (paint one only under html.gfui-standalone);
  2. a zero-sized root (#gfui-root { position: fixed; width: 0; height: 0 }): every piece of UI is positioned against the window and covers only what it paints; no pointer-events declarations.

Desktop, mobile and web: how the page is shown #

┌──────────────────────────────────────────────────────────────┐
│ PAGE (HUD, menus): the same code on every device             │
│  • transparent background                                    │
│  • what the page does not claim (falls on body) → the game   │
│  • keyboard → whoever has the focus; a button gives it back  │
└──────────────────────────────┬───────────────────────────────┘
                 bridge: state ↓   commands ↑   (same API)
┌──────────────────────────────▼───────────────────────────────┐
│ ui.webui: native Rust module, common to the 4 engines        │
│  choice: --webui > GRIPFORGE_WEBUI > game config > auto      │
│  web engine: system | embedded | downloaded                  │
│  transparency: auto | always | never                         │
└───────┬──────────────────────┬──────────────────────┬────────┘
        │                      │                      │
  ┌─────▼──────────┐   ┌───────▼────────┐   ┌─────────▼────────┐
  │ CEF shared     │   │ WRY            │   │ COPY             │
  │ GPU texture    │   │ system web     │   │ (last resort)    │
  │ read as is by  │   │ view, laid     │   │ changed areas,   │
  │ the engine,    │   │ over the game  │   │ nothing when the │
  │ 0 copy         │   │ (Win, macOS)   │   │ picture is the   │
  │ (default)      │   │ 0 copy         │   │ same, ⚠ warning  │
  └───────┬────────┘   └───────┬────────┘   └─────────┬────────┘
          └──── fallback ─────►└──── fallback ───────►┘

The way is chosen once, when the interface starts; a way that cannot work hands over to the next, named in the log. The copy is never chosen while a way without copy works.

Windows #

CEF ── draws into a shared D3D11 texture ──► Godot samples it in place ──► screen
       (Godot on Vulkan or on D3D12: whichever runs, chosen automatically)
fallback: WRY (the system's WebView2, composed by Windows) ─► copy
pointer: the page tells its areas in advance, the game decides at the click
  • Way: cef, the embedded browser, the same as on Linux and macOS. Its picture is a D3D11 texture shared with the game: Godot opens it with the driver in use (D3D12, or Vulkan with VK_KHR_external_memory_win32, which the extension adds to Godot's device). Nothing is copied.
  • Fallback: wry, the system web view (WebView2) laid transparent over the game window, when the engine cannot import the texture (the Compatibility renderer, a Vulkan driver without the extension) or while the embedded browser is being installed. The reason is in F3 (ui.webui.backend, fallback). A missing or too old WebView2 runtime is named (webview2-missing, webview2-too-old).
  • Input with wry: the page receives the pointer and the keyboard first and hands the game what it does not claim. While the game captures the mouse (Godot MOUSE_MODE_CAPTURED) the web view lets everything through.

macOS #

CEF (IOSurface import not written yet) ─► WRY (WKWebView, composed by macOS) ─► copy
pointer: same rules as Windows

Linux #

CEF ── draws into a GPU texture (Vulkan) ──► the game shows it ──► screen
       (embedded or downloaded browser, about 290 MB)
fallback: copy (no Vulkan, driver) ; no WRY
pointer: the page tells its areas in advance, the game decides at the click

wry is not offered on Linux: its web view would be an opaque window hiding the game. --webui=wry there is named and cef is used.

Android and iOS #

Not yet available: the module has no mobile way today. A game exported to a phone has no ui.webui interface yet.

Web #

game <canvas> ──┐
                ├─ same page, composed by the browser ──► screen
HUD DOM ────────┘   0 copy, no download
click: the browser decides alone (on body or the canvas → the game)

Forcing a way #

Priority: launch argument > environment variable > game config > automatic. Every argument has its variable, handy when the command line of the game cannot be touched.

ArgumentVariableConfigValues
--webui=<way>GRIPFORGE_WEBUIbackendauto, wry, cef, copy, off
--webui-strictGRIPFORGE_WEBUI_STRICTstrictno fallback: a forced way that cannot work fails clearly
--webui-cef-d3d12GRIPFORGE_WEBUI_CEF_D3D12cefOnD3d12Windows with D3D12: cef first anyway (by default wry goes first there, cef-d3d12-unvalidated)
--webui-engine=<source>GRIPFORGE_WEBUI_ENGINEengineSourcesystem, embedded, download
--webui-engine-url=<url>GRIPFORGE_WEBUI_ENGINE_URLengineUrlwhere the engine archives are fetched
--webui-engine-dir=<folder>GRIPFORGE_WEBUI_ENGINE_DIRengineDirwhere a fetched engine is kept (a cache shared by several projects)
--webui-scale=<0.5..2>GRIPFORGE_WEBUI_SCALEresolution of the interface
--webui-fps=<n>GRIPFORGE_WEBUI_FPSfpspictures per second at most (cef, copy)
--webui-transparency=<mode>GRIPFORGE_WEBUI_TRANSPARENCYtransparencyauto, always, never
--webui-logGRIPFORGE_WEBUI_LOGthe choice in the log: the way kept, the reason of each fallback

The web engine in an exported game #

The embedded browser (cef) is about 300 MB per platform. It is fetched only for the platform that needs it (this machine when you play or test, the targets of an export when you export), once, then reused:

  • Three modes, all config: downloaded from GripForge (the default, nothing to set), downloaded from your own address (engineUrl), or shipped with the exported game (bundleEngine). While the engine is being fetched, or if the download is unavailable, the system web view is used at once where there is one (Windows, macOS): the first launch always has its interface. The system web view never waits for the engine nor takes its errors; the reason is shown in the debugger (F3) and the engine is used from the next launch.
  • While the game starts (Godot): the download is a loading step (webui-engine): the menu shows its progress, « Play » waits for it when the HUD needs it, the game never freezes.
  • Where: in your project (<project>/webui-engine/, ignored by the editor and by git), or in a folder shared by several projects (GRIPFORGE_WEBUI_ENGINE_DIR or engineDir: one copy for all of them). Never forced onto the system disk.
  • From where: an ordered list of addresses. engineUrl first when you set it (a mirror, an S3 bucket, file:///folder/ or a folder for offline use), then the GripForge distribution (its site). An address that refuses, fails or cannot be reached hands over to the next; nothing empty is left in the cache. The SHA-256 compiled in the module is always checked: a source that serves another file is refused and nothing is unpacked.
  • Export (bundleEngine, off by default): nothing of the engine is put in the exported game. The game uses the engine already installed on the player's machine (a shared install: the user cache, GRIPFORGE_WEBUI_ENGINE_DIR), else the system web view (wry, Windows and macOS: nothing to ship), else fetches it at its first start (Linux, where cef is the only way). No error: the way kept and the reason are shown in the debugger (F3).
  • With bundleEngine: true the engine of each target platform is copied beside the exported executable (webui-engine/<version>-<platform>/): the game starts with it and downloads nothing. Godot does it in its own export (the addon's export plugin, nothing to enable): exporting for Windows adds only the Windows engine. bundleEnginePlatforms limits it to some targets.
TargetAdded to the export with bundleEngine: true (off: 0 MB)
Windows x86_64350 MB
Linux x86_64323 MB
macOS arm64296 MB (inside the .app)
macOS x86_64306 MB (inside the .app)

Transparency #

  • auto (default): the page is blended over the game. A page that paints an opaque background over the whole window by mistake is hidden with a warning, so the game stays playable. A full-screen menu declares itself opaque with data-gf-opaque on <html>: it is shown opaque and the engine is told (ui:opaque), so it may lower or pause its rendering behind.
  • always: always blended, never hidden. never: opaque.
  • A hidden or empty page costs nothing.

Input options #

Keys of the input config:

KeyDefaultMeaning
clickThroughtruewhat the page does not claim goes to the game
bodyEventsfalsea press on the empty document (body) is the page's (full-screen menus)
overlayOnlyfalsethe page takes no input (a HUD that only shows)
hudRoot""a CSS selector: only what is inside it is claimed

A release goes to whoever received the press; the wheel follows the pointer; double clicks, side buttons and the cursor shape of the page work as in a browser; the keyboard (IME and text input included) belongs to the page only while one of its elements has the focus, and the game releases the keys it held when the page takes it; losing the window focus ends every gesture.

In the debugger #

The debug panel (F3, snapshot gripforge.debug/1) shows a ui.webui section given by the module itself: the way kept, the ways tried and the reason of each fallback (with a warning for the copy), where each setting came from (argument, variable, config, automatic), the web engine (system, embedded, downloaded, its version, the download progress), the transparency (transparent, opaque, hidden), the input sharing (pointer captured or free, keyboard owner, owner of the press, hover verdict) and the cost of the interface (sampled). With the panel closed nothing is collected.

Settings and defaults #

KeyDefaultMeaning
pagewebuifolder of the built page
namespace__gfuipage globals namespace
transportautoauto, osr, devws, web, none
autoStatetrueweb: push the game variables to the page
stateHz30web: automatic state pushes per second at most (moddable, 1 to 120)
forwardEvents[]web: bus topics forwarded to the page as events
mounttrueweb: load the page into the document
fragments[]fragments declared by the game or a mod (below)
themetruesend the game theme (ui.theme, --gf-* variables) to the fragments (moddable)
backendautodesktop way: auto, wry, cef, copy, off (see above)
strictfalseno fallback when the way asked cannot work
cefOnD3d12falseWindows with D3D12 (Godot 4.7's default): the shared picture of cef is not validated there yet, so the automatic way starts on wry, cef as its fallback (reason cef-d3d12-unvalidated in F3); true keeps cef first. Temporary
engineSource, engineUrlauto, ""where the web engine comes from (engineUrl: a mirror, an S3 bucket, a local folder for offline use; its SHA-256 is always checked)
engineDir""where a fetched engine is kept; empty = the project (<project>/webui-engine/)
bundleEnginefalsedesktop exports carry the web engine beside the executable (below); off: shared install, else wry, else fetched at first start
bundleEnginePlatforms[]only these export targets carry it; empty = every desktop target exported
fps60desktop cef: pictures per second at most
transparencyautoauto, always, never
inputsee aboveclickThrough, bodyEvents, overlayOnly, hudRoot
mixNativeUifalsedesktop engines: keep the engine's native UI too
rebuildOnRuntrueengines: rebuild the page on every run or compile
devUrl""desktop: load this URL (a dev server, hot reload inside the game)
browserPath""desktop: the browser to drive (or env GF_WEBUI_BROWSER)
devtoolsPort0desktop: 0 = a free port picked by the browser; keep 0 so several games can run side by side
renderScale, everyNthFrame1, 1desktop: image scale and frame skipping, to trade quality for cost
devPort9800Godot devws transport only

Only stateHz and theme are moddable: page, devUrl, browserPath, transport, mount... say which page, browser or port is used, and a mod must not point the bridge at another page.

Input sharing #

What the page does not claim belongs to the game.

  • Keep the page background transparent (a still, fully opaque page is hidden after a few seconds).
  • Off-screen, a press goes to the page only where the last frame is not transparent; a release goes to whoever received the press; moves are never consumed.
  • The keyboard belongs to the page only while one of its elements has the focus; a clicked button gives it back, a text field keeps it until the player clicks the game.
  • On the web, native DOM routing does it all.
  • Unity cannot consume an input event: gate your game input with GfWebUI.GameOwnsPointer / GfWebUI.GameOwnsKeyboard.

Building the page #

ui/build.mjs builds the page with the bundler and copies it where each output serves it; it is incremental (a hash of the sources is stamped in the output). Node.js 18+ is needed on the development machine only; without it the engines keep the last page.

OutputPage served from
Godotres://<page>/ (add <page>/* to the export's non-resource filter)
UnityAssets/StreamingAssets/<page>/
UnrealContent/<page>/ (Additional Non-Asset Directories to Package)
Web export<export dir>/<page>/ with node build.mjs --web <export dir>

Fragments: the web UI of presentation modules #

A presentation module can bring its HUD and menus as a fragment: one file under modules/, ui.mjs exporting React components (the page's React, the module bundles none) and/or templates with data bindings, or a .html template. A module declares it in the web entry of present-modules.json ({ ui, views, vars, shared, emits, layer, origin, sha256 }); the game declares one with the fragments config (one per kit, the last wins).

  • Data in: the fragment receives only the keys it lists (views, vars, shared, theme, themeVars, config, lang, assets, inputs, planned), as objects, with a delta of what changed. The theme also lands as --gf-* CSS variables (var(--gf-accent, <yours>)).
  • Events back: dispatch(event, payload) of a component, or data-gf-on-click of a template, sends ui:emit. Accepted only for an event listed in emits; it becomes ui.command { cmd, payload, kit }, otherwise ui.fragment.refused. A fragment never feeds game input.
  • Direct calls (secrets): call(method, args) reaches a method of the fragment's own kit listed in calls of its web/ui.json. It is never an event: what it carries (a password typed in a sign-in form) reaches no bus, mod, trace or log.
  • Trust: a fragment runs with the trust of the mod scripts of game.engine. origin: "mod:<id>" obeys the mods policy (mods.enabled, mods.disabled, mods.deny.kits); sha256 is checked when given; scripts and on* attributes of a template are dropped.
  • One way per kit: when a fragment draws a kit's interface, that kit's page views are not mounted beside it, so a HUD is never drawn twice. A refused fragment gives the page view back.
  • ui.tree: a module that describes its UI as a tree is drawn as DOM by this kit too; both end in the same layer.

Values that change at every frame #

A compass that follows the camera, a position, an animated gauge: values the HUD shows at every frame never travel as messages. A fragment declares them in frames; the page reads them in place in a shared memory region, in its own animation frame, and moves only the element bound to them with a CSS transform (no layout, nothing else repainted).

<!-- web entry of the module: { "ui": "modules/compass.html", "frames": ["camera.yaw"] } -->
<div class="needle" data-gf-frame="camera.yaw" data-gf-frame-template="rotate({v}rad)" data-gf-frame-period="6.283185"></div>
  • Templates: data-gf-frame="<name>", data-gf-frame-template ({v} = value × data-gf-frame-scale + data-gf-frame-offset, default rotate({v}rad)), data-gf-frame-property (default transform), data-gf-frame-period (an angle: drawn the short way round).
  • Components: props.frame.value('camera.yaw'), or props.frame.bind(el, 'camera.yaw', { template, scale, period }) once (it answers the unbinding). Only the names the fragment declared are readable.
  • Where the values come from, with no code in the game: camera.yaw, camera.pitch (radians), camera.x, camera.y, camera.z from the camera the engine drew the frame with; any other name from the state var of that name. A value published slower than the page draws (a var at the tick rate) is drawn between its two last values.
  • What the game writes itself wins: Godot var s := host.ui_frame_value("hud.boost") once, then host.ui_frame_set(s, value) each frame; C (Unity, Unreal, Rust) gfk_ui_frame_value / gfk_ui_frame_set; web createFrameValuesRegion() of gf-frame-values.js (slot, set, publish). Typed, in place, no JSON.
  • How the region reaches the page: Linux (cef) the browser maps it; Windows (wry) a WebView2 shared buffer posted once; web a SharedArrayBuffer (cross-origin isolated page, else an ArrayBuffer). Where nothing can be shared (macOS, a browser that refuses), the numbers come at most 15 times a second with their speed and the page interpolates. The way and its reason are in the debugger (F3, ui.webui.frameValues: mode, reason, what the page counted).
  • Up to 64 values, names of [A-Za-z0-9_.:-]. Nothing changes for a fragment that declares no frames.

Events and service #

WhatNameDetail
Emitsui.command { cmd, payload }every page command (kit added for a fragment event)
Emitsui.ready { transport }the page is connected
Emitsui.fragment.refused { kit, event, error }a fragment event or call was refused
Listensui.state { data }merged into the state sent to the page
Listensui.event { kind, data }forwarded to the page as an event
Serviceui.webuipushState, patchState, pushEvent, onCommand, receive, state, status, and for fragments declareFragments, feedFragments, setTheme, setFragmentContext, emitFragment, onFragmentEvent, mountFragments

The kit declares no hook. The state and the outbox are not saved: the game pushes its state again after a load.

Engines and limits #

  • The web player and the Godot web export need nothing more than the page.
  • Desktop engines: see "Desktop, mobile and web". The page is served from the files of the package (no HTTP server, no port). Mobile and consoles are not covered yet; Unity WebGL and Unreal HTML5 are not delivered.
  • Native page (the ui.webui presentation module): the engine gives where the files of the game are with the setting game-files = <prefix>=<absolute folder> (the Godot addon sends res://gripforge/=<the gripforge folder of the game>). A path that starts with the prefix in what reaches the page (a bound slot of ui.theme, the title background of ui.menu) is then loaded from that folder only; a .bin image (PNG, WebP, JPEG, GIF) is typed by its first bytes.

Copy, the last fallback (desktop engines) #

When no zero-copy way works on the machine, the native interface falls back to the copy: the browser paints in memory and the pictures are copied to the engine. A warning says so in the log and in the debugger (ui.webui.surface, field fallback). Then only the rectangles that changed are sent, nothing is sent while nothing changes, and the engine updates only those regions of its texture; a full-screen opaque menu is drawn without blending and an empty page is not drawn.

Launch argumentConfigDefaultMeaning
--webui=copyforce the copy
--webui-scale=<0.25..1>scale1resolution of the interface as a share of the window (layout unchanged, picture stretched)
--webui-fps=<1..240>maxFps60pictures per second at most
--webui-transparency=auto|always|nevertransparencyautoblend over the game as painted, always, or never
--webui-strictoffrefuse the copy: no interface rather than a copied one (webui-copy-refused)

GRIPFORGE_WEBUI_TRANSPARENCY sets the transparency too. While the debugger is open (F3), ui.webui.surface shows, a few times a second when they changed, the bytes sent per second, the changed pixels, the identical paints skipped, the time of the game thread per paint and the effective scale, pace and transparency; the time per paint is sampled; closed, nothing is measured.

When it does not do what you expect #

SymptomCause and fix
The page covers the game or eats clicksan opaque background or a root with a size: keep it transparent and zero-sized
Keys do not reach the gamean element of the page keeps the focus: blur it after use
A command of the page does nothingread ui.command on the bus; for a fragment, the event is missing from emits (ui.fragment.refused)
The HUD is drawn twicea native view and a fragment for the same kit: declare the fragment, the view is dropped
Two games clash on a porta fixed devtoolsPort: set it back to 0
The interface disappeared, with a warningthe page painted an opaque background: keep it transparent, or declare a full-screen menu with data-gf-opaque
The log says fallback … cef-copyno way without copy works on this machine: the reason is in the same line and in the debugger

<!-- kit-services:begin -->

Services this kit uses (and that you can provide) #

  • Resolved at run time: <kit> (methods — ; optional)

Any brick can serve them: a kit, or your engine (Godot provide_service, Unity Provide, Unreal ProvideService, Bevy provide_kit_service, C gfk_provide) — see the guide. <!-- kit-services:end -->