I'm trying to develop logic for my game to read inputs and act upon them, and to one day turn it into a C++ library, but I keep hitting some snags.
Has anyone ever created a system, or know of any that exists, that is robust enough for most modern control quality-of-life expectations, while also being modular and comfortable for a programmer to use without requiring dozens of handlers for edge cases?
For the sake of explaining what I mean, let's pretend I'm making a simple game. The player can move left and right, shoot a spitball, and charge up for a jump by holding down an input and releasing it to lift off. Plus pause the game. Let's also skip over the process of translating a hardware input into an abstract action input in an enum, based on the player's customized control scheme (e.g. Spacebar down -> jump action down). I think I've got that part pretty much figured out.
The most obvious answer is to listen to input events and act upon them. But what about an event I shouldn't handle right now? Like the player holding down the jump input, pausing the game, and then releasing the jump input in the pause menu.
- If I ignore the release event, then when the player unpauses the character will keep charging the jump.
- If I listen to it anyway, then the character releases the jump mid-pause menu. Sure, they won't go anywhere since physics are paused, but the propulsion sound will still play, the dust particles will still kick off, and so on. Plus what if after releasing the input the player holds it down again and unpauses? The game should have continued charging from where it left off instead of releasing.
If I instead poll for the state of an input every frame...
- This is obvious for left and right movement, and also works well for jumping, since if the input is down and the character wasn't charging a jump, they can start doing so (and vice-versa). But what about spitting a projectile? Unless I add some specific logic for cases like this I can't stop the character from spitting every frame.
- Plus how do I poll for stateless inputs? Like if somebody binds the spitball input to the mouse wheel scrolling down?
Ok, what if I use a different approach for each type of action?
- This mostly works, but has some caveats too. I want the player to be able to hold down an input during the loading screen, pause menu, or the "Ready... GO!" interlude before action can be taken, and for the action to take effect on frame 1 when gameplay resumes. Via polling this works automatically for something like moving or charging a jump, but since spitting would use events, it would not work.
Plus there are a few painful corner cases.
- If the player is in the pause menu, and hits the B button to back out, the game unpauses and also instantly makes the character throw a spitball because both are bound to the same input. Or maybe they focus on the "Unpause" input and hit A, which unpauses and instantly makes the character hold a jump. Do I want this? Maybe I do for the jump, but not the spitball?
- At any moment I can open the dev console with an input press while the game keeps running. Obviously inputs taken with the console open should be fed to the console and not to the gameplay. But if the player was holding down an input to walk forward when it was opened, the gameplay won't be able to notice the input being released and will make the character keep walking.
I feel like there must be a solution that works for all cases. After all, the premise is simple in theory: in whatever game state you are in, listen to what the player is doing for this game state, and other game states stay paused in time and when they come to, they should listen to what the hardware is saying at that moment. But like I listed above, I can't get it to work. My current solution is close, but is also a mess, filled with specific functions and class members to hack my way around edge cases. There has to be a smarter, more systemic way.
I was curious to see how game engines do it. I took a look at how Godot does it, and it's a bit confusing. Inputs are sent to menus first, and if those aren't marked as handled, they're sent to other menus, and only eventually do they trickle down to gameplay? I couldn't make much sense of it, and either way, it looks like it doesn't do anything to attempt to allay the problems I listed above.
I imagine most projects have simple controls, and don't really bother with amenities like held input actions activating the first frame possible, or the player being able to hold and release inputs during the pause menu and have the action continue from where it left off. Some projects probably do worry about that, but just have a bunch of specific checks for various edge and corner cases. But what about those really complex titles with hundreds of actions of various types, and deeply intricate multi-platform control schemes, like GTA? I feel like I can really build something that could handle these sort of cases gracefully, so that it's easier to develop games with robust input reading and quality-of-life features.
Does anyone have any ideas? Thanks in advance!