Skip to content

Porting Godot 4: Input

Caroline Joy Bell edited this page Apr 15, 2024 · 1 revision

Overview

Implementing input is a mostly freeform system in Godot 4; most of the platform code could be doing pretty much anything, as long as it updates the Input singleton.

Input singleton API:

The input singleton can be split into two general groups: Joypad, and regular events.

  • Keyboard and mouse are implemented as regular events, which means the engine only expects/reacts to one source for those inputs. Inputs are passed to the API in a "This input has occurred" format.
  • Up to 16 independent Joypad sources can be connected at once. Each Joypad has a unique ID, which is retrieved and managed by the singleton. Inputs from joypads are passed to the API in a "This input has occurred on this device" format, with an attached ID referring to the source Joypad.

A couple of notes:

  • Input types for the Joypad and mouse are stored in enumerators, which can be found in core/input/input_enum.h (as of writing this)
  • The input singleton itself is in core/input/input.h. Do not call the Input class for the API directly: Instead, grab a pointer to the singleton via Input:get_singleton() and call functions from that.
Function name Return value Parameters Description
joy_button(int p_device, JoyButton p_button, bool p_pressed) void p_device: The ID of the input device being processed
p_button: A value corresponding to an entry in the JoyButton enum
p_pressed: Whether the button is pressed (true) or not (false).
Processes a standard button input from a joypad device.
joy_hat(int p_device, BitField<HatMask> p_val) void p_device: The ID of the input device being processed.
p_val: A bitfield HatMask value representing which direction(s) are being held down.
Processes d-pad inputs from a joypad device.
joy_axis(int p_device, JoyAxis p_axis, float p_value) void p_device: The ID of the input device being processed.
p_axis: Which axis is being processed, based on the JoyAxis enum.
p_value: The new value of the axis being processed.
Processes an axis input on a joypad device.

What MUST Be Implemented

  • A function to process inputs, which should probably be run every loop within OS:run() for your platform.
  • Your input system must talk to the Input singleton for Godot to process inputs.

What Is Optional

  • Implementing keyboard.
  • Implementing more than one input type (eg. keyboard and joypads). If you are implementing a singular Joypad (eg. handheld), you still need to make sure it is mapped to an ID.