Supporting Gamepads in my LÖVE projects
Working through a class at the same time as I have my kids doing schoolwork at home has its benefits. A big one is that they see me listening to a teacher, doing online exercises, reading, completing assignments, etc. It also nets me a couple of eager play testers for the projects.
I have been surprised to see that needing to play on a laptop keyboard seems to make the games too hard for them. Fortunately, LÖVE seems to have good built-in gamepad support.
Here’s all it took to map a few buttons on a pad to the keys used to control the bird in our flappy bird clone:
local keymap = {}
keymap ["leftstick"] = "8"
keymap["start"] = "return"
keymap["a"] = "space"
keymap["back"] = "p"
function love.gamepadpressed(joystick, button)
mapped = keymap[button] or "unknown"
if mapped ~= "unknown" then
love.keyboard.keysPressed[mapped] = true
end
end
Other gamepad functions need to be accessed using the joystick
table.
That love.gamepadpressed()
function will provide a useful hook for configuring that on projects that have more involved controls.
One thing that really helped figure out how the API works is using a REPL to try things out in my game’s real environment. I forked this one and added back LÖVE 10 support. I will post about using it once I’ve given it a bit more wear.
I’m trying on Kev Quirk’s “100 Days To Offload” idea. You can see details and join yourself by visiting 100daystooffload.com.