Explaining Mad Driver's Input System (Or: How to Make your Input System Reusable)


If you're a game developer (like me), you've probably worked with some type of input system (functions like 'KeyPressed' or some other variation of it). If you want the player's character to move around on the screen, it is kind of essential, so having an input system that functions properly and is easily (re)usable is a must.

In my case (working with Visual Studio using C# and the 'MonoGame'-engine), if we wanted to create a simple input system that lets the player move in four directions, it might look something like this:


Simple, really. Not the greatest piece of code, but it should get the job done. Since this is probably one of the first pieces of code you would write when making a game, it'll likely be left the same throughout your game's development, as your focus may quickly shift to that enemy with its cool attacks you're designing, or the player's respawn which you can't seem to get working, or that UI feature which breaks the game for some reason (no idea why).

However, you'll eventually start running into a problem: if (whilst testing for example) you discover a certain key doesn't work that well, what do you do? For the code shown above, its quite easy (simply change the keys), but what if you have inputs tied to certain actions? What if you have a very complicated input system with a large number of keys? What if your 'action keys' are used throughout the entire game? You'd have to go through all of your code, changing the keys everywhere. Even using a search-function to look for them, having to scan through all of your code is never fun.  

But how do we fix this problem? 

The main issue is that the keys you're using in the project aren't stored anywhere: you only chose a key and made a reference to the engine to check if it's being pressed. Between that, there's nothing to get the specific key from. What we need is something to store the specific keys in and refer to that when checking if they're being used. Looking at the code above, what we can do is store the various keys we see ('Left', 'Right', 'Up' and 'Down') in an array of keys (in the same class, or perhaps somewhere else):


If we then reference them in our code, we get this:


On the surface, it might look like we've actually made the code worse and less understandable (we can no longer see what specific key is being checked), but by restructuring it this way, we actually open up many possibilities with what we can do. For example: if we wanted to limit the player's movement by having it 'lock up' if he pressed multiple keys at the same, what would we do? In the old code, we would've had to create a function with a ridiculous number of conditionals, and it wouldn't be very practical either. Now, using the array, all we need is a few if-statements and a single for-loop:


 This already looks much nicer than seeing '&&' multiple times. 

Is it perfect? No, certainly not. Is it inefficient on a larger scale? Maybe. Can it be improved? Absolutely. Perhaps, with some time and skill, the array could be replaced by a dictionary, which would make it possible see the actual key itself (as a string for example). Whether the function above would still be possible then is to be seen (I'm not aware of all the things that can be done with a dictionary), but it does solve one of the major issues with the version that I've created.

Anyhow, I hope that this 'tutorial' was useful to you. I'll see what other things I can post about on 'Mad Driver', but it'll take some time to see. Thanks for reading!

Get Mad Driver

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.