Gamehoster World Functions

The World is the front-end abstraction. A game that opts in ships its drawing as a folder of small snippets (gamehoster-frontend/) and the page provides only a root element. One call, Gamehoster.start, hands that root over; from then on the library owns the render loop, the input plumbing and the per-object lifecycle, and runs the game's snippets each frame. Those snippets read the whole interpolated world through this same Gamehoster surface: the members below.

Each member is a view or a small accessor on the live Gamehoster global. The getters recompute each loop iteration, so a snippet just reads them.

FunctionWhat it does
Gamehoster.start(rootOrId, join, opts?)Start the abstraction on a root element and return a handle.
Gamehoster.InstanceThe interpolated instance view.
Gamehoster.PlayersA Map of player views, keyed by netId.
Gamehoster.EntitiesA Map of entity views, keyed by netId.
Gamehoster.FramePer-frame timing and tick info.
Gamehoster.Root()The root DOM element the page handed to start.
Gamehoster.PersistentThe game-wide front-end scratch bag.
Gamehoster.connectedWhether the socket is up.
Gamehoster.tickRateThe game tick rate.
Gamehoster.Reconnect()Tear down and restart the whole connection and loop.

Starting the world

Gamehoster.start(rootOrId, join, opts?) is the abstraction entry. The page provides a root element, as a string id or the element itself, and the library takes over: it owns the render loop, the input plumbing and the per-object lifecycle, running the game's gamehoster-frontend/ snippets each frame. join is the join payload the room receives, for example { name: 'Ada' }. opts.game selects which game to start; it defaults to the game baked into the client. If the root id names no element, start throws.

Gamehoster.start('game', { name: 'Pilot123' })
Booting the Asteroids world on a canvas with id game

start returns a handle:

MemberWhat it does
.connThe underlying connection the world runs on.
.stop()Stop the loop, detach input listeners and close the connection.
.onError(cb)Register an error handler; returns the handle so it chains.

Instance

Gamehoster.Instance is a getter that returns the interpolated instance view. The instance's schema fields sit at the top level, so a snippet reads them directly, for example arenaW, arenaH, phase and score. Before the first snapshot arrives it returns {}, so a renderer can test a field to know whether the world is ready.

Players

Gamehoster.Players is a getter that returns a Map keyed by netId, each value a player view. Alongside the standard Map surface it carries one extra member.

MemberWhat it does
.selfYour own player view, or undefined before it appears.
.values()Iterate every player view in the world.
.sizeHow many players are currently in view.

Entities

Gamehoster.Entities is a getter that returns a Map of entity views, keyed by netId. Alongside the standard Map surface it carries one extra member for picking a kind.

MemberWhat it does
.ofType(type)An array of the views whose .type matches, for example the rocks.
.values()Iterate every entity view in the world.

Frame

Gamehoster.Frame is a plain object the loop refreshes each iteration, so a snippet reads it directly to pace its animation and to place ballistic objects.

FieldWhat it does
.numberThe frame counter, rising by one each loop iteration.
.timeThe frame timestamp in milliseconds.
.dtMilliseconds since the previous frame.
.tickThe current render lead tick, used to compute ballistic positions from their start, velocity and spawn tick.
.leadHow many ticks the present leads the newest authoritative data.

Root

Gamehoster.Root() returns the root DOM element the page handed to start, the canvas or div the game draws into. Asteroids grabs it once in setup to cache the 2D context.

var canvas = Gamehoster.Root()
Gamehoster.Persistent.Set('ctx', canvas.getContext('2d'))
setup caches the canvas context from the root element

Persistent

Gamehoster.Persistent is the game-wide front-end persistent bag: client-side scratch that survives across frames, the twin of the server's Instance.Persistent. It has two members.

MemberWhat it does
.Get(name)Read a value from the bag.
.Set(name, value)Write a value into the bag.

Asteroids stashes the 2D context, the eased own-ship flame and heading, and a seed-to-shape cache for the rocks here.

Connection

Three members expose and manage the socket the world runs on.

MemberWhat it does
Gamehoster.connectedA getter for whether the socket is up.
Gamehoster.tickRateA getter for the game tick rate.
Gamehoster.Reconnect()Tear down and restart the whole connection and loop, returning the new handle.

The Asteroids renderer uses all three: when the instance has no state yet it prints connecting while connected is true, and once the connection is lost it invites a click and calls Reconnect() to start over.

if (!Gamehoster.connected && Gamehoster.Mouse.pressed('left')) Gamehoster.Reconnect()
Asteroids reconnects on a click after the connection drops