Game controller library for LÖVR
First require the library: local gc = require "game_controller"
On lovr.load
call getDeviceCount
to get the number of connected devices at the time the application starts.
Ideally, you should also store each device's unique identifier at this point, by calling getDeviceGUID
. This will be useful later because the index (jid) passed into various functions of the lib isn't guaranteed to be the same between different runs of your app, or if a device was connected/disconnected.
The idea is that you can check against your stored GUIDs to correctly identify a specific device.
Another option is to call getDeviceName
but this also isn't guaranteed to be unique. There are cases where some generic gamepads are simply just called "USB Device".
You can check if a device was connected/disconnected at runtime by querying configurationChanged
.
There's a database of known game-controllers maintained by the SDL community. A version of this file is included with this library, but the latest version can be found in this link so that you can replace it anytime.
At the start of the app, the library reads this file to get updated information about these known game-controllers. It is used for mapping buttons/axes of different controllers to a common format. You can query if a connected device is one of these by calling isDeviceGamepad
, and then call getGamepadState
to get the state of buttons/axes using named values instead of indices. (See example in the API below)
The rest are mostly polling functions which are self explanatory.
isDevicePresent( jid )
getDeviceCount()
getDeviceName( jid )
getGamepadName( jid )
NOTE: Always check if the device is a gamepad with isDeviceGamepad
getButtonCount( jid )
getButtonState( jid, bid )
getAxesCount( jid )
getAxisValue( jid, aid )
getHatCount( jid )
getHatState( jid, hid )
- Centered = 0
- Up = 1
- Right = 2
- Down = 4
- Left = 8
configurationChanged()
[1] true if a device was connected/disconnected
[2] string describing the event ("connected" or "disconnected")
[3] index of the device
NOTE: This should normally be called every frame and it will return true only for the particular frame where a change was detected.
getDeviceGUID( jid )
getGamepadState( jid )
NOTE: The returned table fields are buttons
and axes
Each field is an array that can be indexed with named values. Example:
local value = getGamepadState( 1 ).axes[ GAMEPAD_AXIS_LEFT_X ]
In this case the value of the axis named GAMEPAD_AXIS_LEFT_X
, for the device with index 1 is returned.
isDeviceGamepad( jid )