🔒 Locked
Events programming...
Advertisement
Original Post
Ok so I made a class that interfaces with DirectInput (more importantly with the keyboard). I've made a public event where the keystate has changed. My question is how should I program the events? My program will rely a little bit on windows style systems, where you have many objects on screen, but only the object that has the current focus will react to input. So should I remove all methods from the event and place the currently focused object's method in the event or should I just have all of the object's "KeyPressed" methods in the event, and let it cycle through until it reaches the one that has focus?
You could implement an event based system using boost::signals which I've been reading up on all day and will be using the my event/message system.
GCoda:
Sorry man, I'm working in C#. As to the pseudo code you offered, I will be using this code for my GUI, so I will not have the keyboard respond to only one object, otherwise that would be a very good and valid suggestion.
Antareus (Is that Orion?):
Thanks for the code but I understand C or C++ code about as much as I do Japanese (or the computer equivalent... Assembly *bad memories*). But, what I get from your code is that you cycle through each of the objects until the one that is currently supposed to react to input (or in focus) will get the keyboard input. Is that correct?
Saruman:
Like I said before. I dunno crap about C or C++ which seems to be what boost::signals is about, but I'll have a look and see if I can port anything from there in to C#. Thanks for your help.
EDIT: Ise reallyz gots ta workz on my engelischz.
Sorry man, I'm working in C#. As to the pseudo code you offered, I will be using this code for my GUI, so I will not have the keyboard respond to only one object, otherwise that would be a very good and valid suggestion.
Antareus (Is that Orion?):
Thanks for the code but I understand C or C++ code about as much as I do Japanese (or the computer equivalent... Assembly *bad memories*). But, what I get from your code is that you cycle through each of the objects until the one that is currently supposed to react to input (or in focus) will get the keyboard input. Is that correct?
Saruman:
Like I said before. I dunno crap about C or C++ which seems to be what boost::signals is about, but I'll have a look and see if I can port anything from there in to C#. Thanks for your help.
EDIT: Ise reallyz gots ta workz on my engelischz.
I suppose that (iterating through all objects to determine selected ones) is one method of checking to see what is selected, although from a programmer's perspective I would think it easier to keep a list of the currently selected items that changes as the user clicks on them. Then when it comes time to modify the selected items you can just apply a transformation to them and go.
A good first first thing to do is to separate the physical key input from the semantic action interpereted by the program.
So you might have an enum of all actions like:
enum EActions {
eMoveRight,
eMoveLeft,
eMoveForward,
eTurnRight,
eTurnLeft,
...
}
and then one function that maps the incoming keypress into an action. Then your program's objects only respond to these actions. For example, you might want to the left arrow to mean "Turn left". But you might also want the letter 'a' to. And you might want the numeric keypad #4, also. You don't want your game logic to be worried about these possibilities, so you do all the mappings in one place and only make game objects respond to "eTurnLeft".
This doesn't really answer your specific question, but it is a wise path to take on the way.
So you might have an enum of all actions like:
enum EActions {
eMoveRight,
eMoveLeft,
eMoveForward,
eTurnRight,
eTurnLeft,
...
}
and then one function that maps the incoming keypress into an action. Then your program's objects only respond to these actions. For example, you might want to the left arrow to mean "Turn left". But you might also want the letter 'a' to. And you might want the numeric keypad #4, also. You don't want your game logic to be worried about these possibilities, so you do all the mappings in one place and only make game objects respond to "eTurnLeft".
This doesn't really answer your specific question, but it is a wise path to take on the way.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement