🗓️
Oct 12, 2024
Designing the input system for my game engine
Creating an easy to use input system for my game engine, similar to Unreal Engine

I have never been a fan of input systems where you just use a bunch of if statements to check if a key is down, like unity's legacy input system. For my engine I decided to create an Input API similar to the legacy Unreal Engine input system, where you bind callbacks to specific key states.
Here is code from my editor camera's BeginPlay function, you can see it looks pretty similar to binding inputs in Unreal Engine.
The entire Input system is less than 300 lines of code between both the header and translation unit. I simply have structs that contain the input event, and input type. These are stored in a hash map, where the key is the hash of the struct, this is simple since SDL input types already support std::hash. The value in the hash map is just a vector of std::function.
The input system has a ProcessEvent() function called from SDL::PollEvent(), it has a switch case for each of SDL's input types, and then just indexes into the hash map and calls every std::function in the returned vector.
I am pretty happy with how well the system works for how simple it is, but ideally I want it to work with some sort of InputAction prefab like Unreal Engine's new Input System, this would make it much easier to organize Keyboard/Gamepad inputs together, and allow for easy rebinding. Would also be nice to have a macro over std::bind to make the code nicer to read.