Handling Input
Input is something that always seems just slightly easier than it seems.
When you first look into the documentation for the framework you’re using, you’ll undoubtedly see methods that you can use that will instantly make input work with very little effort on your end. As tempting as it may seem, it will cause you problems down the road. For one thing, gamers generally don’t take too kindly to when they cannot remap their controllers to their preferred control scheme. And why not? it’s the 90s.
Input is actually still easy, but in order to save you some time, let me give you some tools that will help you down the line.
How Input is Generally Handled
Generally, most game frameworks will have at least two ways to handle input; either by callback or by scanning. If it doesn’t have both, it will almost certainly have at least the scanning method.
With the scanning method, the framework will have a map of which buttons are pressed somewhere in memory and will offer methods to check if the button is down or not. Generally one would check sometime between frames to determine what actions to take. In some ways this is great because we are seeing what’s happening to our buttons in realtime and can react to them accordingly. But on the other hand, it doesn’t tell your program when the button was pressed - or just as importantly, when it gets released. So if that’s your only option for input, you’ll find yourself writing a lot of support code for it. If not you’ll get odd things happening like scrolling through a menu one item per frame - not exactly user-friendly!
The callback method offers you a lot of that work done for you. LÖVE has such methods, like love.keypressed()
or love.joystickpressed()
. These methods fire when any keyboard key or joystick button respectively are pressed, passing along which exact key or button caused it to fire. Using such a thing with that menu example would mean that you could easily have the menu advance by just one item per button press, no matter how long you hold it down.
With that in mind, if you have the choice between these two methods, the optimal choice for which to use in your project is really not set in stone; one is not inherantly better than the other, and it’s actually fairly often that you’ll need to use a hybrid approach. Many games tend to need chorded input, where the action of pressing a button is altered by holding down another. In such cases, the handler for your callback might want to use the input scanners to see if that chording button is pressed.