Skip to content

Command

Jiufen edited this page Mar 23, 2021 · 2 revisions

🕹Command Pattern🕹

The command pattern will rely the execution of certain actions to a command processor and the logic of that actions to each command. The pattern also allow us to have a persistence of this actions, this is the major utility of this pattern. For example, if we want to store the actions that a player did during a match in order to save a replay of that match, we will probably use the command pattern.

In this example we are going to make an undo functionality of the movements the player does. For this we will use this infrastructure:

The character will read the input for the player movement, adding every command of the movement to the command processor and then executing it. Then when we want to undo this movements we simply tell the command processor to cycle through the list of movements from the newer one to the last one, and execute them.

The code can be seen in this link

Clone this wiki locally