1
\$\begingroup\$

I'm doing some experimentation on XNA, and unlike past experiences with XNA, I wanna do stuff right.

Are there any good patterns for managing user input?

Like... how do I manage what components get the input?

Is there anything like that? I feel like doing the normal - checking for state of a button and if-ing for every thing you need... It can't be that there isn't something more clever and well designed to do that.

I feel that the wheel has already been invented here, I just can't find it.

\$\endgroup\$
1
  • \$\begingroup\$ Check out the Command Pattern. The entire book there is now free to read, I highly recommend it. The example are C++ but I learnt a lot from it to refactor my MonoGame code. \$\endgroup\$ – lozzajp Aug 2 '16 at 14:58
2
\$\begingroup\$

A common way is to have a controller base class. This can be an AI controller or just an input controller that is looking at gamepad or keyboard input.

In general adding an extra layer of abstraction makes sense. So you'd have inputs specific to your game, like jump, left, right, up, down, etc. This will allow you to do keybindings easily, since your game only needs to know about what the controller outputs.

The code you are describing in your post still has to run somewhere, but you can make it more comfortable to use.

If your question is about how to handle wether a menu or the player or whatever is currently handling the input. You can just have an Active switch inside things that use the input to turn them on or off.

Your question is pretty vague, so I'm not sure if this clears it up.

\$\endgroup\$
1
  • \$\begingroup\$ You are correct, my questions is pretty bad and not specific enough. I'm currently not programming the game itself, but instead implementing a basic EntityComponentSystem library that would meet my needs. I could then do an input controller component interface and implement it differently as I please \$\endgroup\$ – Giora Guttsait Aug 2 '16 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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