Key Binding & Input

Sep 29, 2009 at 2:26pm
I have a very complex key binding and input system I have created that is for the most part hardcoded and the player cannot modify any settings. There are actually 3 keybinding/input subsystems that are used, each depending on the state of the game, menu, etc... I am now starting to break out the functionality to allow the player to select an action (function) and bind a specific key to the function. So I have a growing list of functions and a static list of keys and key combinations that the player can bind.

What I was thinking was to have an array of function pointers and include function pointers to the functions I will allow the user to bind to, for example: run, walk, or jump. So if a user selects the jump action and hits the space bar, I would update the key to use that function pointer. I have not tried something like this before as I tend to avoid function pointers for simplicity. What would be the best way to execute this and how do you execute a function pointer that's in an array? Also, any alternate design ideas are welcome!

Return 0
Sep 29, 2009 at 3:21pm
Actually, instead of an array, I would suggest an std::map (with the key being whatever the user inputs). Anyway, to call a funcptr, it is basically like calling any other function, except you have to derefence the pointer first. e.g.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef void (*FuncPtr)(void);
//this makes FuncPtr point to a function that looks like:
// void func();
//If you need a pointer to a class member function, you do it like this:
typedef void (someClass::*ClassFuncPtr)(void);

int main() {
    std::map<keyType, FuncPtr> KeyMap;
    //get key or whatever
    if(KeyMap.find(key) != KeyMap.end()) {
        (*KeyMap.find(key))(); //derefence the function pointer, then call it like normal
    }
    //...
    return 0;
}


There was a nice guide I remember reading to understand it, but I don't have the link.
Sep 29, 2009 at 4:47pm
Hmm... I can see how that works. I always look for the simplest approach. I have another idea. What if I assign a unique id to each function and keep them all in a switch. So for example, lets say the walk action has a unique id of 1. If the player wanted the a button to walk he could access the binding menu and pick the action. In the game I would just update a or whatever key to the appropriate unique id that ties to the action. So when my input subsystem is called and the key pressed is passed, it would automatically know which function it should execute using the switch. A barebones example would be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int a = 1;
int b = 2;

void Switch( int code )
{
	switch(code)
	{
	case 1:
		{
			walk();
			break;
		}
	case 2:
		{
			run();
			break;
		}
	}
}


What do you think? Wouldn't this approach eliminate the need for function pointers all together and STL containers?
Last edited on Sep 29, 2009 at 4:51pm
Sep 29, 2009 at 5:14pm
Yes, that would work as well, I think std::map's lookup is faster, but if you put the most used actions at the top you could make the speed faster. (It would probably look cleaner too).
Topic archived. No new replies allowed.