Skip to content

Input Handling

Hyomoto edited this page Oct 18, 2020 · 15 revisions
Jump To back Overview Contents Macros

Module Overview

The Input Handling module is concerned with combining and consolidating inputs. It provides an interface for generic input types, a generic input device, as well as wrappers for gamepad, mouse and keyboard inputs. Lastly, it can automatically handle connection and disconnection of gamepads.

Dependencies: Core

Contents

Creating an InputDevice

InputDevices are generic containers for control mappings. Rather than polling the keyboard or gamepad directly, you can simply name your inputs whatever you wish and then assign the inputs you want to them. Here is an example of a simple, two-input controller:

global.controller = new InputDevice( "shoot", "jump" );

global.controller.shoot.bind( new KeyboardKey( vk_control ) );
global.controller.jump.bind( new KeyboardKey( vk_space ) );

We can now check the status of our inputs:

if ( global.controller.shoot.pressed() ) {}

InputDevices support the following checks: pressed(), held() and released().

NOTE: When pressed(), held() or released() is called, the initial state of the button will be set. This means that calling pressed() or held() multiple times in the same frame could yield unexpected results. If you require checking the same button multiple times in a single frame, it is recommended to cache the initial query, or create multiple InputDevices.

Creating a Gamepad

Gamepads are a little like input devices except they have to be created before they can be used:

global.gamepad = new GamepadXbox();

It is then possible to poll the controls on it the same as you would an InputDevice. However, gamepad inputs can also be fed into InputDevices to combine them with other types of inputs:

global.controller.shoot.bind( global.gamepad.a, new KeyboardKey( vk_space ) );

In this case we bind the A button on our gamepad and the spacebar to our shoot control. Gamepads are automatically managed, connections and disconnections are handled without further input from the user.

Macros

Name Type Value Description
GAMEPAD_MAXIMUM_VIRTUAL_PORTS int 4 The number of virtual ports gamepads can plug into
Clone this wiki locally