Input

Scripts ask about actions by name. The project maps each action to keys, mouse buttons and gamepad controls, so the keys can change without touching a script.

The input map

The map is in the project file:

Input:
  Jump: [Space, Gamepad A]
  MoveX: [D, -A, Right, -Left, Gamepad LeftX]
  MoveZ: [S, -W, Down, -Up, Gamepad LeftY]

A minus in front binds a control the other way. That is how two keys make an axis: D pushes MoveX to 1 and -A pushes it to -1. Both held together cancel.

Set an action with a command:

{"command": "input.set_action", "name": "Fire", "bindings": ["Mouse Left", "Space", "Gamepad RightTrigger"]}

Names

DeviceNames
KeyboardA to Z, 0 to 9, Space, Enter, Escape, Tab, Left, Right, Up, Down, LeftShift, LeftControl, LeftAlt and their Right forms
MouseMouse Left, Mouse Right, Mouse Middle, Mouse 4, Mouse 5
Gamepad buttonsGamepad A, B, X, Y, LeftBumper, RightBumper, Back, Start, LeftThumb, RightThumb, DpadUp, DpadDown, DpadLeft, DpadRight
Gamepad sticks and triggersGamepad LeftX, LeftY, RightX, RightY, LeftTrigger, RightTrigger

In a script

Lua
TypeScript
function Player:OnUpdate()
	local x = Input.GetValue("MoveX")          -- -1 to 1
	if Input.WasPressed("Jump") then end        -- true for one frame
	if Input.IsDown("Fire") then end            -- true while held
end

Ask WasPressed in OnUpdate. A fixed update can run twice in a frame, or not at all.

Mouse

Lua
TypeScript
local dx, dy = Input.GetMouseDelta()       -- movement this frame
local x, y = Input.GetViewPosition()       -- 0 to 1 across the game's picture
Input.SetCursorLocked(true)                -- for a camera that looks around

Use GetViewPosition with a camera's ScreenToRay or ScreenToGround to find what the mouse points at:

Lua
TypeScript
local ground = camera:ScreenToGround(Input.GetViewPosition())

GetViewPosition is the same in the editor's viewport as in the game's own window. GetMousePosition is in window pixels, which differ between the two.

Rebinding

Lua
TypeScript
Input.SetBindings("Jump", { "Enter", "Gamepad B" })
Settings.Save()

SetBindings lasts until the game stops. Settings.Save keeps the player's bindings for the next run.

For a "press a key" screen, Input.GetLastPressed() returns the name of the key or button that went down in this frame.