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 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
In a script
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
export = DefineScript({
OnUpdate() {
const x = Input.GetValue("MoveX"); // -1 to 1
if (Input.WasPressed("Jump")) {} // true for one frame
if (Input.IsDown("Fire")) {} // true while held
},
});
Ask WasPressed in OnUpdate. A fixed update can run twice in a frame, or not at all.
Mouse
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
const [dx, dy] = Input.GetMouseDelta(); // movement this frame
const [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:
local ground = camera:ScreenToGround(Input.GetViewPosition())
declare const camera: Camera;
const [x, y] = Input.GetViewPosition();
const ground = camera.ScreenToGround(x, y);
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
Input.SetBindings("Jump", { "Enter", "Gamepad B" })
Settings.Save()
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.