Skip to content

Input Handling

Hyomoto edited this page Nov 29, 2020 · 15 revisions
Jump To back Overview Contents Macros Index

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 lot like InputDevices, except they specialize in one specific form of input: the gamepad. This is largely because, for example, GMS doesn't support multiple keyboards, but it does support multiple gamepads, and gamepads have to handle connections and disconnections. Thus, we need a reference to a specific gamepad:

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. However, if you are expecting a gamepad to be used and want to access the unique characteristics of the gamepad inputs, you can do so directly. Buttons support pressed(), held(), released() and magnitude(), whereas an axis (ie: joysticks) support degree(), magnitude(), horizontal(), vertical(), left(), right(), up(), and down().

Lastly, if you would like to bind a joystick direction as a generic input, you can do so using InputMethod:

global.controller.left.bind( new InputMethod( global.gamepad.lstick.left ) );

Macros

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