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.
| Function | What it does |
|---|---|
Gamehoster.start(rootOrId, join, opts?) | Start the abstraction on a root element and return a handle. |
Gamehoster.Instance | The interpolated instance view. |
Gamehoster.Players | A Map of player views, keyed by netId. |
Gamehoster.Entities | A Map of entity views, keyed by netId. |
Gamehoster.Frame | Per-frame timing and tick info. |
Gamehoster.Root() | The root DOM element the page handed to start. |
Gamehoster.Persistent | The game-wide front-end scratch bag. |
Gamehoster.connected | Whether the socket is up. |
Gamehoster.tickRate | The 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' })
start returns a handle:
| Member | What it does |
|---|---|
.conn | The 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.
| Member | What it does |
|---|---|
.self | Your own player view, or undefined before it appears. |
.values() | Iterate every player view in the world. |
.size | How 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.
| Member | What 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.
| Field | What it does |
|---|---|
.number | The frame counter, rising by one each loop iteration. |
.time | The frame timestamp in milliseconds. |
.dt | Milliseconds since the previous frame. |
.tick | The current render lead tick, used to compute ballistic positions from their start, velocity and spawn tick. |
.lead | How 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'))
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.
| Member | What 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.
| Member | What it does |
|---|---|
Gamehoster.connected | A getter for whether the socket is up. |
Gamehoster.tickRate | A 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()