In my game, the 'z' keyboard key serves many functions. Depending on the situation, it can be used to kick, shoot, reload, talk to an NPC, continue a conversation, end a conversation, or select items in a menu.
I've been using a combinations of conditions to determine what code should be executed when 'z' is pressed. They check for player 'state' (instance variable) values, object visibility, overlap with other objects, layer visibility, or a combination of these. It works but is getting messy, and when I add new features or make changes to the game, my logic is prone to break. It's basically spaghetti logic. I'm pretty early in my game so I have to think there's a better way to implement this, and that this is a common challenge with some proven solutions.
Is there a 'best practice' on how to implement and manage overloaded key events? Maybe use of a data structure or dictionary opens up some cleaner options?
I have the same problem, so I make myself use the e key too.
Develop games in your browser. Powerful, performant & highly capable.
Since posting this I realized I can use Event Groups for this, and enable/disable groups based on the situation. E.g., I can have a Player Attack group with all the keyboard events I use for attacks, and disable this when interacting with a menu. This is a lot cleaner than the individual conditionals I've been using for every keyboard event. I'm looking forward to seeing how this will work.
Any other 'best practices' folks have been using?