0
\$\begingroup\$

Currently I am starting to develop my first serious game. Before the games are all training, and learning how to do things in a good way. Unfortunatly, I've missed a big part in my preparation: Key binding.

Currently I am trying to figure out how I can bind keys to action for each scene in my game. For example: If the user presses the JumpButton during the GameScene, the avatar should jump, but pressing the same key in a MenuScene, the currently selected menuitem should be activated.

Which key the JumpButton is, should the user be able to set. But the behaviour should be always the same.

Here is my first, and I think very naviive idea:

I am having a HashMap with HashMap where Key is the key on the Keyboard and ACTION is just an enumValue. When I am in the scene, and a button is pressed, I will take that button, and ask the HashMap which action is bind to the button. using a switch I can now determine what to do. In my mind there is a little gap.. I have ONE action per Button, maybe named: ACTION_PLAYER_JUMP as an enumValue, and when I am in a GameScene this is okay, but in a MenuScene this ACTION_PLAYER_JUMP is not a very nice name... I do not want to have all bindings two times, just for better names.

Can someone tell me how to fix this, or a better system? I am sure there is a much better system for handling those bindings.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You should think of how to generalize this system so that any key can perform any action. Not only can the player customize their keys, but you might also want to enable different key behaviors between different menus.

You should be switching out the different keyboard mapping states when you switch scenes/to a new menu/etc. So when you enter a menu, the 'in-game' map values get removed and you add the 'in-menu' values. For this you could have previously defined lists of actions that are to be enabled/disabled at particular points eg:

RegularGameplay[jump,run,moveForward,shoot]

You can then call an enableSystems(List,bool) function that takes the list and adds/removes the actions on the list from the Map.

This sort of procedure becomes much more important as your game grows in complexity and different sections of the game require different specific mappings (specially when it comes to different menus).

I would however store something different in the hashmap values. You could have straight-up just a function pointer/delegate, and when a key is pressed you just call the function stored as a value for that key. You could also have an 'InputAction' class, and each action the user is able to perform is a new instance of this. An InputAction instance can hold things such as the function to call when the action is to be performed, a 'string' nice name to show the user, or even which key it's currently/should be mapped to (useful if the keys are customizable, so you know which key you should map an action on activation).

The base line is allowing for a particular subset of actions to be added/removed from your Map as the game state changes, and not try to have one single static map.

\$\endgroup\$
1
\$\begingroup\$

Your mapping strategy sounds like it'll work great. The problem you really need to solve is having pretty display names for your enum values.

See this question and answer on SO.

Essentially, you can switch from a enum or you can use C# properties to save and display additional information about each enum value.

Using either option, your dictionary is still going to use some kind of key mapping from a key code to an action. The dictionary is an ideal data structure for this kind of operation.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.