Tag: Game Design Patterns

Using Command pattern to handle input in Unity and C# (Part I )

by on Nov.24, 2015, under Unity3D

Definitely Command is one of my favorites patterns. It is present in most of my games code-base. When used in the right place will help you to achieve encapsulation of your Input -> Action code.  Let’s see first how the “Gang of four” define this pattern:

Encapsulate a request as an object, thereby letting users parametrize clients with different requests, queue or log requests, and support undoable operations.

As you can see that definition is just … obscure? (I can’t find the right words for it). Well for all non-native English speakers (as myself) I would find that sentence a bit difficult to understand at first. “Encapsulate a request as an object” we all can agree that is referring to and object oriented encapsulation of callbacks (or at least kind of). With “Letting users parametrize clients with different request” we can assume that is referring to the ability to use the objects mentioned before to perform different actions. Then finally the rest is just a list of things you can do with this pattern.

Let’s now get down to business. In all games, or at least 99,9999% of them, there is a place where the code reads the user input (game pad, keyboard, touch screen, …). Once this input is received, translates this information into a game action, for our example let’s say jump and fire:

control

(continue reading…)

Leave a Comment :, , more...