A Lua binary module for Garry's Mod that exposes XInput (1.3) functions
This hooks into Garry's Mod's xinput1_3.dll
that is provided with any build.
XInput is far better than DirectInput (the alternative of getting controller input in GLua), because DirectInput maps both triggers to one axis, making them indistinguishable if both are in use. XInput does not have this issue. Additionally, XInput has rumble support. Finally, this module has a threaded poll loop. That means that if your game freezes momentarily, your inputs will still be registered! They will also record the instant that they occurred.
If you are running Garry's Mod in 32-bit (this is not asking about your machine architecture; Garry's Mod is a 32-bit game and, at the time of writing, become 64-bit if and only if you are on the x86-64 branch), place gmcl_xinput_win32.dll
in garrysmod/garrysmod/lua/bin
. If the bin
folder does not exist, create one. If you're running 64-bit, place gmcl_xinput_win64.dll
in the same location.
This module will not work properly if Steam (or some other program) is eating your controller inputs. This will happen when Steam takes your controller inputs, and maps them to keys and mouse movements (instead of actual controller inputs). In order to fix this, you must use the "Gamepad" controller configuration template. This will pipe your controller inputs into XInput. To enable this, enter the "Controller configuration" menu while in-game, select "BROWSE CONFIGS", go into "Templates", and select "Gamepad." You can also set up any controller detected by Steam to act as an XInput controller, by binding each button to the actual button you want to be pressed in XInput.
This module adds the following hooks and functions:
For all events, when
is the RealTime()
at which it occurred.
Called when controller number id
has been connected. Note: to save on computing time, controllers that are disconnected will only be polled every 4 seconds. It may take longer for the reconnect to be detected.
Called when controller number id
has been disconnected.
Called when button
(see XINPUT_GAMEPAD_* enums) on controller number id
has been pushed down.
Called when button
(see XINPUT_GAMEPAD_* enums) on controller number id
has been released.
Called when trigger
(0 is left) on controller number id
has been moved to new position value
. value
is between 0-255 inclusive.
Called when stick
(0 is left) on controller number id
has been moved to new coordinates (x
, y
). Each coordinate, x
, and y
, are between -32768 - 32767 inclusive.
xinput.getState(id
) → XINPUT_GAMEPAD
/false
Gets the state of controller number id
. This returns a XINPUT_GAMEPAD struct, or false
if the controller is disconnected.
Gets whether the button
(see XINPUT_GAMEPAD_* enums) on controller id
is currently pushed down.
Gets the current position of trigger
(0 is left) on controller id
. Returns a value between 0-255 inclusive.
Gets the current coordinates of stick
(0 is left) on controller id
. Each coordinate is between -32768 - 32767 inclusive.
Attempts to get the current battery level of controller id
. If successful, returns the level between 0.0-1.0 inclusive. Otherwise, returns false
, and a message explaining why it couldn't get the level.
Returns a table where each key is the id
of a controller that is currently connected (the values are always true
).
Sets the rumble on controller id
. Each percent value is between 0.0-1.0 inclusive. XInput controllers have two rumble motors: a soft motor, and a hard motor. This allows for many different combinations of how the rumble "feels".
Name | Value (hexadecimal) |
---|---|
XINPUT_GAMEPAD_DPAD_UP | 0x0001 |
XINPUT_GAMEPAD_DPAD_DOWN | 0x0002 |
XINPUT_GAMEPAD_DPAD_LEFT | 0x0004 |
XINPUT_GAMEPAD_DPAD_RIGHT | 0x0008 |
XINPUT_GAMEPAD_START | 0x0010 |
XINPUT_GAMEPAD_BACK | 0x0020 |
XINPUT_GAMEPAD_LEFT_THUMB | 0x0040 |
XINPUT_GAMEPAD_RIGHT_THUMB | 0x0080 |
XINPUT_GAMEPAD_LEFT_SHOULDER | 0x0100 |
XINPUT_GAMEPAD_RIGHT_SHOULDER | 0x0200 |
XINPUT_GAMEPAD_A | 0x1000 |
XINPUT_GAMEPAD_B | 0x2000 |
XINPUT_GAMEPAD_X | 0x4000 |
XINPUT_GAMEPAD_Y | 0x8000 |
Type | Name | Description |
---|---|---|
number |
wButtons |
A bitmask containing all buttons pushed. See XINPUT_GAMEPAD_* to see which bits correspond to which button. |
number |
bLeftTrigger |
The position of the left trigger, between 0-255 inclusive. |
number |
bRightTrigger |
The position of the right trigger, between 0-255 inclusive. |
number |
sThumbLX |
The X-coordinate of the left thumb stick, between -32768 - 32767 inclusive. |
number |
sThumbLY |
The Y-coordinate of the left thumb stick, between -32768 - 32767 inclusive. |
number |
sThumbRX |
The X-coordinate of the right thumb stick, between -32768 - 32767 inclusive. |
number |
sThumbRY |
The Y-coordinate of the right thumb stick, between -32768 - 32767 inclusive. |