150k views
3 votes
Have you ever wondered how a game implements a configurable control scheme? Of course, there are many solutions, but a common one is to associate an input command (keyboard, mouse, or gamepad) with an action (usually a piece of code). This requires you to store a list of mappings.

E.g:

W key -> Move forward

S Key -> Move backward

In this assignment you will create such a mapping using delegates to store the function to be called. In C++ you would have used a function pointer to accomplish this.

Download and run the DelegateKeypress sample code. You will see a game hard coded with the arrow keys to move the player around on the console.

There are TBD comments explaining the changes you should make to the code. You can either create the mappings in code or you can ask the user to press the 4 required keys to set up the mapping.

The data structure you need is quite simple. You need a structure that stores the ConsoleKey and the action to be run. Then you need a collection of those since there are many mappings. Choose a collection that maps to the usage.

Then during the main loop you will search the collection looking for an entry that matches the key that was pressed. If you find one you will call the action through the delegate.

User Penn
by
8.1k points

1 Answer

6 votes

Final answer:

In game development, configurable control schemes can be implemented by associating input commands with actions using delegates, which require a structure to store console keys and actions, and a collection to handle these mappings.

Step-by-step explanation:

The task described is to create a configurable control scheme for a game using delegates in a programming environment, which stands as a replacement for function pointers used in C++. The process involves setting up associations between input commands (like pressing the W key) and actions (code that makes a character move forward, for example). This requires a structure to hold these associations and a collection to manage multiple mappings.

To achieve this, you could define a structure with a Console Key to represent the game input and a delegate to hold a reference to the action that needs to be executed. Then, you would use a collection, such as a dictionary in C#, to map each Console Key to its corresponding delegate action. During the game's main loop, when an input is received, you would search the collection for the matching key and invoke the corresponding delegate to perform the action.

The configuration of the mappings could be hardcoded in the code or set up dynamically by requesting the user to press keys to determine the controls. This helps in making the game user-friendly by allowing them to customize their controls according to their preferences.

User Daren Chandisingh
by
8.6k points