Making games

Input

Scripts read input from Input. You can ask about a key directly, but a game is easier to change, and easier to let players rebind later, if it asks about actions instead: jump rather than Space.

Actions

Actions are listed in config/input_actions.otter.json in your project, and File → Project Settings → Input shows them. There are two kinds:

  • A button is on or off: jump, shoot, interact.
  • An axis runs from -1 to 1, from a key for each direction: walking left and right, forward and back.
{
  "name": "Input Actions",
  "actions": [
    { "name": "horizontal", "kind": { "Axis": {
        "positive": [{ "Key": "D" }, { "Key": "Right" }],
        "negative": [{ "Key": "A" }, { "Key": "Left" }] } } },
    { "name": "jump",  "kind": { "Digital": { "bindings": [{ "Key": "Space" }] } } },
    { "name": "shoot", "kind": { "Digital": { "bindings": [{ "Mouse": "Left" }] } } }
  ]
}

An action can have several bindings; any of them works.

Reading input

fn jump() { log.info("jump") }
fn fire() { log.info("fire") }

fn on_update(delta: float) {
    var h = Input.axis("horizontal")       // -1 to 1
    if Input.pressed("jump") {             // the frame it went down
        jump()
    }
    if Input.held("shoot") {               // every frame it's down
        fire()
    }
    if Input.released("jump") { }          // the frame it came up
}

held, pressed and released take an action’s name, a key’s name ("Space", "R", "Escape") or a mouse button ("MouseLeft").

The mouse

Input.mouse_position() is where the pointer is, in pixels from the top left of the game, and Input.mouse_delta() is how far it moved this frame: use that for a mouse-look camera. Both are a Vec2.

fn on_update(delta: float) {
    var look = Input.mouse_delta()
    log.info("turned {look.x} across, {look.y} down")
}

Every call is listed in the Cog API reference.