Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm designing a system to handle the keyboard input for my game. To get it more simple, I have a class that has two methods, KeyUp and KeyDown which tell me which key has been pressed, this is already implemented by the library I'm using, now I want to have a way to send that information to the classes that are handling input (I'm using an MCV pattern, those would be the controllers). I've thought of two ways of doing that, both seem valid to me, and I don't know which is better, so I'm asking which would be the best way, in terms of object-oriented design, and if it has any significant impact, also efficiency.

The first way I thought of is having the class which receives the input hold references to all the controllers, and then call some methods of those controller classes in an event-like fashion when a key event is triggered in this class.

The other method is the other way round, the controllers hold a refference to the input handler class, which in this design has some kind of data structure (I was thinking of an EnumMap) that holds the status of all the relevant game keys, and then the controller classes might query the input handler for a key with some getters (just to encapsulate the data structure).

So, that's it, I'd like some help on how to do this. Any comments on which of both is better, or a new design that might be better than this are more than welcome.

Thanks in advance.

share|improve this question
2  
Asking "Which is best" is pretty much asking for opinions/discussion. You're just suffering a little design indecision. Just pick one and go with it. If you find that the strategy you picked is inefficient, try something else. –  Byte56 Jul 30 '13 at 22:46
1  
Why is efficiency given as a concern and not flexibility, maintainability, etc.? Are you having efficiency problems? Making "the best OOP" solution is also not super valuable, especially as you've yet to determine if OOP is even the right solution for the problem. In general, try to get things working, then reapproach them only if you run into real, identified problems. –  Sean Middleditch Jul 31 '13 at 7:01
    
@Sean Well, as a newbie I often find myself asking the wrong questions (surprisingly, to get right answers, but I'm not talking about that right now). So yes, I was more concerned about flexibility, maintainability and making things easy to use and work with than anything else, although I try to make everything as efficient as I can, so I was concerned about efficiency as well. So I mainly want to know how to do things well, and not that much about object oriented (although I value and like this way of designing software, and try to stay with it as much as I can). –  Setzer22 Jul 31 '13 at 20:40
add comment

1 Answer

up vote 1 down vote accepted

Possibly duplicated with this post

Essentially you are asking the differences between polling and interrupt/event-driven. You can Google it and there are a lot of answers out there. They both have advantages and disadvantages and which one to use depends on the scale and architecture of your application.

Here is another discussion you can look at.

share|improve this answer
    
This should probably be a comment. It's basically a link only answer. –  Byte56 Jul 31 '13 at 0:15
    
Sometimes it's just about knowing how your problem is called, so you can go and look for it. You really helped a lot, and by what I've been able to read with some searching I think I'll stay with the even-driven approach. I mark this as answered although Byte56 pointed out that this might have to be a comment, but it's the only answer, and certainly gave me what I was looking for. –  Setzer22 Jul 31 '13 at 20:41
    
thank you @Setzer22, I will pay attention next time :) –  QQending Aug 2 '13 at 5:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.