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'.
| Member | Returns | Description |
|---|---|---|
Gamehoster.Mouse.x · .y | number | Pointer position in pixels from the top-left of the root. |
Gamehoster.Mouse.width · .height | number | The root's size in pixels. |
Gamehoster.Mouse.fx · .fy | number | Position as a fraction 0 to 1 from the top-left. |
Gamehoster.Mouse.cx · .cy | number | Position in pixels from the centre. |
Gamehoster.Mouse.nx · .ny | number | Square proportion from the centre: ±1 is half the shorter axis, so the longer axis runs past ±1. |
Gamehoster.Mouse.angle | number | atan2 from the centre to the pointer, in radians. |
Gamehoster.Mouse.dx · .dy | number | The movement this event, and the pointer-lock delta under capture. |
Gamehoster.Mouse.inside | boolean | true while the pointer is over the root. |
Gamehoster.Mouse.captured | boolean | true while the pointer is locked. |
Gamehoster.Mouse.capture() · .release() | — | Turn pointer lock on or off. |
Gamehoster.Mouse.drag | object or null | While a button is held, a snapshot of the point it went down in every space plus its button; otherwise null. |
Gamehoster.Mouse.down(button) | boolean | true while that button is held. |
Gamehoster.Mouse.pressed(button) | boolean | true on the one frame the button went down (an edge). |
Gamehoster.Mouse.released(button) | boolean | true 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
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.
| Member | Returns | Description |
|---|---|---|
Gamehoster.Keys.down(k) | boolean | true while that key is held. |
Gamehoster.Keys.pressed(k) | boolean | true on the one frame the key went down (an edge). |
Gamehoster.Keys.released(k) | boolean | true 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
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.
| Member | Returns | Description |
|---|---|---|
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 })
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.
| Member | Returns | Description |
|---|---|---|
Gamehoster.Input.Set(name, value) | — | Feed the named local input the prediction body reads. |
Gamehoster.Input.Get(name) | the value | Read the current value of the named local input. |
Gamehoster.Input.Set('thrusting', true) // prediction sees it this frame
const held = Gamehoster.Input.Get('thrusting')
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
}
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.
| File | Runs on |
|---|---|
gamehoster-frontend-input-mouse-move.js | The pointer moving; under capture it carries the lock delta in Mouse.dx/dy. |
gamehoster-frontend-input-mouse-enter.js · -mouse-leave.js | The pointer entering or leaving the root; Escape releasing a capture fires leave. |
gamehoster-frontend-input-mouse-button-<left|middle|right>-<press|release>.js | A mouse button going down or coming up. |
gamehoster-frontend-input-keyboard-<down|up|left|right|space|enter>-<press|release>.js | An arrow, space or enter going down or coming up. |
gamehoster-frontend-input-keyboard-<letter|number>-<press|release>.js | Any letter or any digit; the character is in Gamehoster.Event.key. |
gamehoster-frontend-input-keyboard-any-<press|release>.js | Any 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 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 }
}