Understand Command Pattern.

Suleiman Abdullah
4 min readOct 31, 2024

--

Objective: Understand Command Pattern.

A command Pattern , is behavioral pattern that let you record actions or events of your game or application and also allow you to Isolate the ability of your features into their own classes.

How to Implement Command Pattern.

1.Create Interface for command pattern.

Command pattern take Interface which define a contract, which take two methods, one for execute action and another for undue .But its not limited to this method only.

2.Create command behavior script.

In this step it depend how many actions you want to record, each action need to be command behavior script. For me it will be move up ,down, right and left.

3. Inherit only ICommand Interface to Command behavioral script.

Do this to all the script

4. Add constructer to command behavioral.

We need to add constructor to command behavioral, but before doing this we will need to create two global variable to able to able to make the player move using this constructor.

In order to make the player move we need transform and speed.

Note: Repeat this step to all command behavioral

5.Implementing Command behavioral script.

In this step we need to add logic to Execute and Undue method in each script according to the movement direction, we do this using a transform dot translate and add vector direction multiply by speed and deltatime.

To reverse player to previous position we will move player to opposite direction.

Repeat this to all script.

6.Create player script and add our 4 Input keys, lets assume the player has speed variable for moving the player.

5. Using Polymorphism to create Instance of each command script.

First create 4 variable of type ICommand, then create new instances of MoveCommands according to the Direction in each input. But this Instance will require a player transform and speed of player.

Now we need to Execute the action, we do this by call execute using our move variables.

6.Record the action each time we move the player.

To do this we need to create Command Manager script, then we will create a list of ICommand. Create a public method with ICommand parameter, then add that parameter to command buffers list.

Go to player script then add action to the command buffer list after each time we call execute.

7.Create Rewind System and playback system

Rewind System

To create a rewind system and I will create a Rewind coroutine and public method called Rewind. Then start coroutine to Rewind method.

To play our list of action in reverse we need loop then use a linq function called Reverse, we access it by calling Enumrable.Reverse and pass our command buffers.Then we call Undue method, to make the movement look smooth I will use WaitForEndOfFrame.

Playback system

I will create like above step but I wont use Enumerable I will use command Buffers as collection. Then I will call Execute method.

Last step I will create a two button and call responsible method every time I press those button.

This is the end of this article “command Pattern”, see you in the next article.

Suleiman out.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development

No responses yet