Game Design Pattern: Command Pt.3- Game Dev Series 138

Objective: using Command Design Pattern to create a Player movement system.

We have known how to use Command design pattern to record the steps of color changing on objects. In this tutorial we will create a 3D object movement controlling system by using Command design pattern. And also can record the movement record to replay and rewind.

Concept

The idea of using Command design pattern to create a player movement system is sending Player gameobject and movement value to the direction command script in each direction, each direction command script would only handle 1 direction.
By sending the record of movement to Command Manager in each step, it would allow user to replay the steps that they moved, and also can rewind back to the origin position.

ICommand and player setting

Create a cube in the center of scene, then create buttons of Play and Rewind.

Create an interface script called ICommand, which is the same setting as what we did in the previous tutorials.

Then create a Player script. This would detect the inputs of player to move the cube. Within this script, we would send the movement command to each command scripts that handle the specific direction movement.
First, create ICommand variables for each direction, and a float variable to adjust the speed.

Inside Update(), using if statement to detect inputs for each direction.

Till here, we need to create the command scripts for each direction first.

Command scripts

Create 4 command scripts for each direction.

Implement ICommand interface in each command script, which means they all require an Execute() and Undo() method.
And also create a constructor to pass in transform of Player and movement speed from Player script.

In here, I would use MoveDownCommand as example.

In Execute() of each script, depends on the direction that script handling, set a movement to player by using Translate.

This is the Execute() of MoveDownCommand.

Once finished setting each command script, we need to call Execute() in Player script.
Using variables of ICommand to each script, then send the transform of Player and movement speed.

Now we can test the result of controlling Player with Command design pattern.

Record the movement of Player

The next part would be using Command Manager to memorize how Player moves. This part would be quite similar to the setting in the example of previous tutorials.

Create an empty object with a CommandManager singleton script. Then create a list of ICommand to store each steps of movement with a public method.

Then we would create the function of playing or rewind the whole list.
After using the namespace of System.Linq, we can create these 2 coroutine easily.

And before assigning these methods to buttons, we need to create the function of rewind to Undo() of each command script.

This would be quite simple, make _player moves to the opposite direction from where it towards in Execute().

Again, I use MoveDownCommand as example. You should adjust each Undo() in every command scripts.

Now we can assign the methods of CommandManager to buttons.

The final step of this function, is record every input in Update() of Player script.

Now we are ready to see how Command design pattern works perfectly with Player movement.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store