Game Design Patterns

5 min. read

Game Design Patterns


Interfaces: Use interfaces so that your code works with any class that implements it, instead of just the one that does today.

Observers and messaging: lets 2 parts of the game talk to each other so that tomorrow, it can be 3 or 4.

Flexibility lets us change our game quickly and development speed is absolutely vital for getting to a fun experience! You cant design a fun experience on paper, it comes with, it demands, iteration and experimentation! The aster you can try out ideas and see how they FEEL, the more you can try and the more likely you are to find something great

“is that it’s easier to make a fun game fast than it is to make a fast game fun”

Excerpt From: Nystrom, Robert. “Game Programming Patterns.” iBooks.

Command Pattern
Gang of Four: Commands are an object-oriented replacements for callbacks

A Command represents a thing that can be done

Create base class of Command, then all the other Actions use command base class to Execute everything.

Make sure every data modification goes through a command.

Configurable input
User input, like keyboard, mouse clicks, etc..
Users can swap out buttons for different actions
Create a base class
Class Command
public virtual execute()
Class JumpCommand : Command
public override execute(){
private void jump(){}
Class FireCommand : Command
Public override execute(){
Private void fire();
Class InputHandler{
Public Command buttonX;
Public Command buttonY;
Public Command buttonA;
Public Command buttonB;
if(Input.GetButtonUp(“X”)) buttonX.execute();
if(Input.GetButtonUp(“Y”)) buttonY.execute();
if(Input.GetButtonUp(“A”)) buttonA.execute();
if(Input.GetButtonUp(“B”)) buttonB.execute();

Side note: instead of check if each button is NULL, we can create an object which execute function is empty

Commands can be combined with queueing with Event Queues
Event Queue use producers and consumers
If we make commands serializable, we can send these commands of the network. Which is important for multi-player games.

Undo and Redo
Class Command{
Public virtual execute()
Public virtual undo()

Cache the properties of the object before the execute function is called, this way, undo can be used. Store only the properties that you need, not the entire object.

For redo, use a list, with a pointer at the current state.

JavaScript example:
function makeMoveUnitCommand(unit, x, y) { var xBefore, yBefore; return { execute: function() { xBefore = unit.x(); yBefore = unit.y(); unit.moveTo(x, y); }, undo: function() { unit.moveTo(xBefore, yBefore); } }; }

Unity implementation
Test if it is better to have a class, which is instantiated or having a behaviour attached to the gameobject. This could cause the GC to run and cause spikes in performance.
Flyweight Pattern
Share data between objects

Observer Pattern
The Subject, can notify the Observers of any changes.
Use Actions or Delegates and Events.

Prototype Pattern

Singleton Pattern

State Pattern

Hold the application’s core data and state, such as player health or gun ammo.
Serialize, deserialize, and/or convert between types.
Load/save data (locally or on the web).
Notify Controllers of the progress of operations.
Store the Game State for the Game’s Finite State Machine.
Never access Views.
Can get data from Models in order to represent up-to-date game state to the user. For example, a View method player.Run() can internally use model.speed to manifest the player abilities.
Should never mutate Models.
Strictly implements the functionalities of its class. For example:
A PlayerView should not implement input detection or modify the Game State.
A View should act as a black box that has an interface, and notifies of important events.
Does not store core data (like speed, health, lives,…).

Do not store core data.
Can sometimes filter notifications from undesired Views.
Update and use the Model’s data.
Manages Unity’s scene workflow.