Web

Client only. HTML pages over the game, from a file your resource ships, kcds://resources/<resource>/<path>, or an http(s) address. A page sends with window.sendToGame(name, json), and what you send with Web.emit arrives as a window event with the data in event.detail. A resource can work only with its own views. Through Events.on, your resource also hears events about its views, each giving one object with `viewId`: webViewOpened (url), webViewLoading (url, isMainFrame), webViewReady (url, when the main frame has loaded), webViewFailed (url, description such as ERR_CONNECTION_REFUSED, errorCode, isMainFrame), webViewNavigated, webViewConsole (message, source, line), webViewFocus and more. A missing file gives ERR_HTTP_RESPONSE_CODE_FAILURE, as does a kcds:// file the resource runtime did not answer within 30 seconds, so a page that cannot load always reports it. To reload a failed page, see navigate.

web.open()

Web.open(url, options = {})

Opens a web view and returns its id. Throws while the browser is not ready. A view ends with `close`, its resource stopping, the player's connection ending, or a restart of the resource runtime after a browser failure, after which your scripts load again and can recreate it.

web.close()

Web.close(viewId)

Closes a view.

web.show()

Web.show(viewId)

Shows a view.

web.hide()

Web.hide(viewId)

Hides a view, which keeps running.

web.focus()

Web.focus(viewId, focused = true)

Gives a view the keyboard and mouse, or takes them back. Meanwhile, the third-person camera does not turn with the mouse.

web.anyFocused()

Web.anyFocused()

Whether a shown view of any resource has the keyboard and mouse, which lets you hold off your controls while the player works in another resource's page. It is read at each call, with no event.

web.isVisible()

Web.isVisible(viewId)

Whether a view shows.

web.setBackground()

Web.setBackground(viewId, background = true)

Keeps a view running without drawing it.

web.isBackground()

Web.isBackground(viewId)

Whether a view runs without being drawn.

Web.navigate(viewId, url)

Loads another page into a view.

web.resize()

Web.resize(viewId, width, height)

Resizes a view.

web.move()

Web.move(viewId, x, y)

Moves a view on the screen.

web.setViewWorldPosition()

Web.setViewWorldPosition(viewId, point, offsetX = 0, offsetY = 0)

Makes a view follow a world point. Its top-left corner sits on the point each frame, moved by the offsets, and it hides while the point is behind the camera or off screen. null returns it to its own position.

web.on()

Web.on(viewId, eventName, handler)

Listens for an event a page sends with window.sendToGame(name, json). The handler receives the parsed payload.

web.off()

Web.off(viewId, eventName, handler)

Removes a handler, or all of an event's if none is given.

web.emit()

Web.emit(viewId, eventName, payload)

Sends an event to a page, where it arrives as a window CustomEvent with the payload in `detail`.

web.screenSize()

Web.screenSize()

The game window's size in pixels. It changes with the resolution, and `gameResolutionChanged` tells you when. Full-screen views and those with `anchor: 'center'` follow it on their own; see open.