Gamehoster Input Functions

The front-end abstraction hands every snippet a Gamehoster library. This page covers its input side: the live Mouse and Keys state you read each frame, the Command sender, the Input feed your prediction body reads, the current raw Event, and the per-event input bodies that run on each pointer and key event. You act by sending commands to the server; you observe the effect in the next World frame, never a return value.

Mouse

Gamehoster.Mouse is the live pointer state. It gives the position in several coordinate spaces at once, so you read the one that suits the job; the methods report button state, with edges cleared at the end of each frame. A button is 'left', 'middle' or 'right'.

MemberReturnsDescription
Gamehoster.Mouse.x · .ynumberPointer position in pixels from the top-left of the root.
Gamehoster.Mouse.width · .heightnumberThe root's size in pixels.
Gamehoster.Mouse.fx · .fynumberPosition as a fraction 0 to 1 from the top-left.
Gamehoster.Mouse.cx · .cynumberPosition in pixels from the centre.
Gamehoster.Mouse.nx · .nynumberSquare proportion from the centre: ±1 is half the shorter axis, so the longer axis runs past ±1.
Gamehoster.Mouse.anglenumberatan2 from the centre to the pointer, in radians.
Gamehoster.Mouse.dx · .dynumberThe movement this event, and the pointer-lock delta under capture.
Gamehoster.Mouse.insidebooleantrue while the pointer is over the root.
Gamehoster.Mouse.capturedbooleantrue while the pointer is locked.
Gamehoster.Mouse.capture() · .release()Turn pointer lock on or off.
Gamehoster.Mouse.dragobject or nullWhile a button is held, a snapshot of the point it went down in every space plus its button; otherwise null.
Gamehoster.Mouse.down(button)booleantrue while that button is held.
Gamehoster.Mouse.pressed(button)booleantrue on the one frame the button went down (an edge).
Gamehoster.Mouse.released(button)booleantrue on the one frame the button came up (an edge).
const aim = Gamehoster.Mouse.angle          // where the pointer points, from centre
if (Gamehoster.Mouse.down('left')) fire()   // held now
if (Gamehoster.Mouse.pressed('left')) tap() // this frame only
Reading pointer position and button edges each frame

Keys

Gamehoster.Keys is the keyboard state, captured on the window so the root need not hold focus. A key k is a KeyboardEvent.key string, such as "ArrowLeft", " " for space, or "w". Like the mouse, its edges clear at the end of each frame.

MemberReturnsDescription
Gamehoster.Keys.down(k)booleantrue while that key is held.
Gamehoster.Keys.pressed(k)booleantrue on the one frame the key went down (an edge).
Gamehoster.Keys.released(k)booleantrue on the one frame the key came up (an edge).
if (Gamehoster.Keys.down('ArrowLeft'))  turn(-1)
if (Gamehoster.Keys.pressed(' '))       jump()   // one edge per press
Reading held keys and press edges

Command

Gamehoster.Command(name, params) is the one generated sender. It looks up the named command your game declares and validates params against its schema: an unknown command name throws, an unexpected param key throws, and each param you pass is type- and range-checked. It then encodes the command and sends it, throttled by the rate for that command in settings.

Commands run on the server on the next tick through the game's own handler. You never get a return value; you observe the effect in the next World frame.

MemberReturnsDescription
Gamehoster.Command(name, params)Validate params against the named command's schema, then encode and send it, throttled by the command's rate in settings.
Gamehoster.Command('aim', { angle: Gamehoster.Mouse.angle })
Gamehoster.Command('thrust', { on: true })
Sending a command, validated against its schema and throttled by settings

Input

Gamehoster.Input feeds and reads the live local input that your own object's client predict body (gamehoster-player-predict.js) reads, for example thrust held or aim angle. Set feeds a value in; Get reads the current value. Feeding it is what makes your own object move the instant you act, while the same values reach the server as commands.

MemberReturnsDescription
Gamehoster.Input.Set(name, value)Feed the named local input the prediction body reads.
Gamehoster.Input.Get(name)the valueRead the current value of the named local input.
Gamehoster.Input.Set('thrusting', true)          // prediction sees it this frame
const held = Gamehoster.Input.Get('thrusting')
Feeding and reading a prediction input

Event

Gamehoster.Event is set only while a per-event input body runs, and is null at every other time. The body already knows its event from its file name; Event carries the detail that name leaves open: the key that fired a keyboard-letter or keyboard-number body, and the button of a mouse event.

{
  "key":    "g",   // ev.key, for a keyboard-letter or keyboard-number body
  "button": 0      // ev.button, for a mouse event
}
The Event object during an input-body run

Per-event input bodies

Input is a folder of small bodies in gamehoster-frontend/, one per event. Each is named for the event it runs on and runs on that event; the client attaches a DOM listener only for a family (mouse or keyboard) that has at least one body, so a pointer-only game takes no key listener and a game with no input body leaves the keyboard and pointer untouched. Inside a body you read Mouse or Keys, send with Command, and feed prediction with Input.

FileRuns on
gamehoster-frontend-input-mouse-move.jsThe pointer moving; under capture it carries the lock delta in Mouse.dx/dy.
gamehoster-frontend-input-mouse-enter.js · -mouse-leave.jsThe pointer entering or leaving the root; Escape releasing a capture fires leave.
gamehoster-frontend-input-mouse-button-<left|middle|right>-<press|release>.jsA mouse button going down or coming up.
gamehoster-frontend-input-keyboard-<down|up|left|right|space|enter>-<press|release>.jsAn arrow, space or enter going down or coming up.
gamehoster-frontend-input-keyboard-<letter|number>-<press|release>.jsAny letter or any digit; the character is in Gamehoster.Event.key.
gamehoster-frontend-input-keyboard-any-<press|release>.jsAny key; it fires alongside the specific body above, with the key in Gamehoster.Event.key.

Asteroids aims on mouse-move and thrusts on the left button. The move body sends a throttled aim and feeds the same angle to the prediction:

// gamehoster-frontend-input-mouse-move.js
var a = Gamehoster.Mouse.angle
Gamehoster.Command('aim', { angle: a })
Gamehoster.Input.Set('angle', a)
The Asteroids move body: aim at the cursor, and feed the same angle to prediction

The settings file gamehoster-frontend/gamehoster-frontend-settings.json holds rate, which throttles a command to that many sends a second and coalesces to the latest, and mouseCapture, which turns on pointer lock. Under capture the library locks on a button press, hides and recentres the cursor, reports movement as Mouse.dx/dy, and releases on Escape; Gamehoster.Mouse.capture(), .release() and .captured control and report it.

{
  "mouseCapture": true,
  "rate": { "aim": 20 }
}
The settings file: capture the pointer, and throttle aim to 20 a second