Game Design Pattern: Command Pattern | Part I

Alex Tolson
2 min readDec 15, 2023

--

First let’s set up our scene:

We have 3 cubes in our scene. Cube 1 is positioned at (-4,0,0). Cube 2 is positioned at (0,0,0). Cube 3 is positioned at (4,0,0).

Now, we will add 4 buttons for the UI. Name the buttons Play_button, Rewind_button, Done_button, and Reset_button.

Expand these buttons and change the text to Play, Rewind, Done, and Reset respectively.

The Command pattern uses a Command Interface to implement two methods: Execute and Undo.

First, we will define our Interface.

And, before we go willy-nilly programming, let’s break down the steps involved with our application.

Our goal is that when we left click a cube, a ray is cast; if a cube is hit, a new random color will be given to cube that was hit.

Let’s first fill in the pseudocode:

This program works:

The Command Pattern allows for decoupling of this code. The separation of action and reaction. When we click, this is code we want to execute:

NOTE: Organization is key.

That concludes this section of Game Design Pattern: Command Pattern | Part I. In Part II, we will create our command script which will inherit from the ICommand interface we created today. We will also get the rest of our buttons working.

Thanks for Stopping By.

--

--