How to Unity
A Command Pattern Example in Unity
A command pattern use case in Unity
A command is a reified method call.
— Robert Nystrom, Game Programming Patterns
Game Programming Patterns is an awesome book. I always keep it on my desk to occasionally use it as a manual.
Design Patterns are solutions to common problems in software engineering or design. Being “patterns”, they suggest a way to solve a known issue, which you can implement virtually in any language.
Some time ago I wrote about the Singleton Design Pattern, you can find it here:
Among the many patterns described in the book, the Command Pattern was the one that struck me the most — it stuck in my mind, as it appeared as the solution to many problems I faced in game development and it was also the funniest to implement.
In the Command Pattern, a request or callback is encapsulated in an object, rendering it more object-oriented.
Think of user input in a game:
- if button1 is pressed do this
- else if button2 is pressed do that