Game Design with Command Pattern

A digital game verb

Jason Li
Dev Genius

--

This is the 1st entry in the Game Design with Programming Patterns series looking at the game design side of programming. The interactive part of this series can be found here: https://ovenfresh.itch.io/design-with-patterns

The underpinnings of action | Photo by Lynda Sanchez on Unsplash

What is the pattern?

The Command Pattern is the strategy of converting a method call into an object. Instead of directly calling a method to perform a function, you create an object that can call a generic execution function. In games, these objects can be linked to specific button inputs or triggers to create spaces where commands can be exchanged with relative ease. It also creates the structure that you need to have redo/undo functionality and also command/input history. This gives us the ability to trace back what actions happen in a game, and inspect them, or even replace them.

How I Used It

Controlling a cube with shuffling Command inputs

Commands pattern serves to create objects of action, so the scene uses a set of commands in a shuffle bag. The Commands for moving forward, backward, left, and right are randomly assigned to one of the four input buttons. Clicking on them moves the cube around. A Shuffle button lets one change the command that each input has been assigned. Then, an Undo button lets the commands reverse course, and remove the entries into the command stack. This gets at most of the basics of Command functionality: actions in a stack, reassignment, and the undo.

Design Impressions

Commands succeed best when they have a single purpose that they act on

The Command pattern puts actions into discrete units, so Command objects turn functionality and behavior into objects with history. Adding history to action gives clear benefits over the ahistorical counterparts: actions can be inspected for the order of execution, their effects can be separated, and they can be undone. Treating them like a part of a digital game’s objects lets the actions be transferable and moveable. Any case where the action is discrete and clearly defined, a command is a perfect thing to implement.

Going Forward

Verbs are one of the fundamentals of game design: play needs action. Command readily translates those concepts of game verbs into programming. A verb can be applied in several scenarios whether they make sense or not, and a command can be used like that too. Creating the Command doesn’t necessarily limit what it can do, setting the groundwork for imaginative verb usage within a game. Commands support the type of drop in and drop out types of grammar one might expect of design verbs. Exploring this expansive property of Commands makes it powerful for experimentation in digital games.

Next: Flyweight

Code: https://github.com/jasonzli/game-programming-study

Reference: Game Programming Patterns, Nystrom, Robert 2014 http://gameprogrammingpatterns.com/command.html

Game programming patterns in unity with C#, Nordeus, Erik 2020 https://www.habrador.com/tutorials/programming-patterns/1-command-pattern/

--

--