Let’s put one more pattern to our piggy bank and see how a one named “Command” can make our life easier.
Control is an integral part of the game. Eliminate control and, at best, the game will turn into a movie. Although recently, even a movie begins to provide the ability to control what is happening on the screen, for example, Black Mirror: Brandashmyg or Mosaic by Stephen Soderberg. Like almost anything in programming, the basic creation of control in the game is not something complicated. You just need to insert a call to the function that checks the click on the left mouse button, and you have set up the shot. Then call up another function and, by pressing the space bar on the keyboard, your character begins jumping. Spread a dozen more calls all over the code, and we can go to production. Add control settings to the game? It is now possible to use Xbox/DualShock controllers in iOs, and it would be nice to add their support? Do you need to port the game to the console? No, we didn’t agree on that. So, it is clear that a simple task can always turn into a difficult one due to a small change in requirements, and in the case of setting up control, the “Command” pattern can help you smooth out the growth of complexity.
The official description of the template says that a command is a wrapper object over the call. This method allows you to transfer commands between objects easily, reconfigure their behavior, and create sequences from them. For a deeper understanding of the theoretical part, you can refer to this chapter from a Robert Nystrom book or this article.
In their essence and basic implementation, commands are very similar to the strategies that we examined in the previous article of the cycle about templates, but their usage varies in scope. The command turns various actions into objects; as a result, you can freely interact with them; for example, for the cancellation of an operation. A strategy usually describes a change in the internal behavior of a particular entity.
Command objects make it easy to create sequences from them, which can be used to undo/redo actions by steps, all you need to do is to supplement the command object with data about reverse actions. It is not very applicable to action games; however, it can come in handy in a turn-based strategy or creating an editor to configure some game elements.
As a practical example, we will use the showcase from the previous article about strategies and, with the command pattern, add the control to the magician. Let’s create three commands: use a spell, switch between spells, and spawn an enemy.
|
|
|
|
|
|
|
|
|
|
It is easy to see that actions abstracted from the calling code. That simplifies the creation of settings or the extension of control methods.
We live in a time when mobile devices, in addition to the standard and often inconvenient control via the touchscreen, receive support for accessible game (and not only) controllers, and the game’s release rarely happens only on one platform. Also, control settings have always been an element of player friendliness. To facilitate the fight against these challenges, the command pattern that we have examined is quite suitable. However, don’t forget that overuse of patterns can turn your code into lasagna. See you next time! =)