Game Design with State Pattern
By: Jason Zhen Li
Tags: Game Development, Software Development, Game Programming, Game Design, Indiedev

Game Design with State Pattern

The conditions of games

This is the 7th 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

Photo by Ryan Quintal on Unsplash

What Is The Pattern?

The State Pattern allows objects to change their internal behavior depending on their internal conditions, or “state”. An object will have a state sub-object handle its outward behavior. State pattern is often but not always joined by an implementation of a State Machine, another type of object that manages the state sub-objects. No matter the method of implementation, the pattern lets objects transition between several states while maintaining the appearance of a single object. While one state might be receiving input, another state might ignore the input and perform an animation. Rather than combine these two modes into one object, the State pattern cleanly separates behaviors into easier-to-understand States with singular concerns.

How I Used It

The entire “Game” controlled with state here. It only receives input when it is done animating. Otherwise, it ignores input and does some setup or clean up behavior.

Behavior chains are one of the immediate ways to apply states. In the scene, the cubes can do a three-part jump sequence dependent on state. They have states for Idling, Jump, Spin Jump, and the Slam. Every jump input moves the cubes through each state. And it maintains their transitions too: a cube cannot go from Jump to Slam, it has to go to Spin Jump first.

The game Board itself is also managed by state. It has the Setup, Play, and Exit states. Setup brings the board in and turns on the input handling for the cubes, enabling the jump sequences above. Exit turns them off and takes the board away. State lets us reinforce a design idea here: “We need game pieces set up before we can play.”

Design Impressions

State implementation can range from simple to complex depending on the requirements

A successful State pattern provides clarity about how and why behaviors occur. However, for all of the State’s gains in organizational simplicity, the actual implementation can prove to be a challenge for inexperienced coders, involving the creation of State Machines in addition to States themselves. Additionally, creating transitions can be difficult without a full picture of how states interlock. The implementation complexity is directly proportional to the complexity of the problem one is trying to tackle. A strong design diagram of states is indispensable when implementing the pattern.

Going Forward

Beyond code, the state pattern exists all over game design. Designers frequently refer to game conditions as game state. Even the basic phases of games like chess can be described this way: “opening,” “midgame”, “endgame.” Knowing the state pattern enables a designer to connect those state descriptions into the actual coded form of the games. Directly coding these states allows the implementation of a broad spectrum of game qualities. It can be applied to game conditions, to behavioral systems, to the specific minutiae of game input. State has a home in nearly every part of the game coding process, and especially in the parts that handle complicated human input. State readily addresses the complexity of our designs, revealing the flow and influence between different elements of a digital game’s structure.

Previous: Singleton

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

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