Key event
Hello, I want to make my game better by having an event when a key is pressed, such as if I press the up key it will call a function that I assign that key like UpKeyPress(); I was wondering how could I do this? Is this even nessesary? I have the code to say the key has been pressed, I just don't want a lot of if statements in my game loop. Also I don't want the key press to keep saying its pressed, like on my tetris game is just moves the block clear to the side if i press the left or right key. I could just probably keep a variable saying if its been pressed dont call the event until its been released. So what do you guys think. Is this something I should spend time with?
I am using c++, directinput, but I would like a design that could be written in any language and api.
One mechanism to create a fairly flexible binding between key events and actions is to use a mechanism that maps keysyms to functions or function objects. An example in C++ would be a std::map<KeySym, boost::function<void (void)> >. This eliminates the need for switch or if statements as well as making it possible to have customized controls.
For other languages you replace the map with whatever type the language has that supports associative arrays. For example, in Python you might use a dictionary.
For other languages you replace the map with whatever type the language has that supports associative arrays. For example, in Python you might use a dictionary.
Im not too knowledgeable with maps or even to begin, I know that you have a value and a key or something. So would I just Iterate through the map and if it came upon lets say DIK_UP it would call the function that I assign it. Should I just have a default function for all the keys or should I have an empty map and just add it as I go?
A map can just be indexed with the key type. For example:
This assumes that even unasigned keys are assigned a null function. If you prefer not to fill in a function for all the keys, you can use action_map.find() to get an iterator, and if the iterator is valid then call the value of the result.
std::map<KeySym, boost::function<void (void)> action_map;// fill in the action mapKeySym up = /* whatever the keysym for up is */;action_map[up]();
This assumes that even unasigned keys are assigned a null function. If you prefer not to fill in a function for all the keys, you can use action_map.find() to get an iterator, and if the iterator is valid then call the value of the result.
Quote:Original post by cptrnet
Im not too knowledgeable with maps or even to begin, I know that you have a value and a key or something. So would I just Iterate through the map and if it came upon lets say DIK_UP it would call the function that I assign it. Should I just have a default function for all the keys or should I have an empty map and just add it as I go?
The key is the map's index: e.g.
map[key] <-- functor element
(*map[key])() <-- executes the functor (if your functor is contained as a pointer)
The reason std::map is good is that you don't have to iterate through it to find your key... just do this:
if(mymap.find(key) == mymap.end()) return false;
That indicates the key wasn't found. Otherwise, it's there!
You don't have to add a functor for a key if you don't want to; if the key has no functor, it will just return false.
[EDIT] - OOps... SICrane is faster than me [smile]
Just make sure you search for your key first...
if you just do this:
map[key], and nothing is in the map at key... you'll add a null node! (bad)
make sure you always search:
if(mymap.find(key) == mymap.end()) return false; // or something to that effect.
if you just do this:
map[key], and nothing is in the map at key... you'll add a null node! (bad)
make sure you always search:
if(mymap.find(key) == mymap.end()) return false; // or something to that effect.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement