Command by

Encapsulates a request as an object, thereby letting users parameterize clients with different requests, queue or log requests, and support undoable operations.

Motivation

In games command pattern is often applied to:

  • Create rebindable input controls;
  • Support undo or time rewind mechanics;
  • Decouple playable characters from direct user input by having player input create identical commands to that of an AI. This enables the game to switch control of characters between different players or AI and reduces the complexity and size of the codebase, by forcing developers to use common code for both player and AI actions.
  • One way of synchronizing multiplayer games is to pass all the input of different players over the network and run simulation on all ends separately. Timestamped commands can be used to make sure all the actions are applied in correct order in all ends of the communication. This is more popular in RTS games with large numbers of units, as instead of synchronizing the complete state of every unit every frame, only a few commands have to be sent over the network.

 

Outside games commands are often used as an object-oriented replacement for callbacks. Callbacks are functions that are registered somewhere and can be called at a later point. As command pattern turns actions into objects, this means commands can be stored, executed at a later point, undone or even passed on to a different process over network.

Structure

 

Command:

  • Is an interface or an abstract class.
  • Contains an abstract or a virtual method for executing the command.
  • The goal of this abstraction is to enable the Invoker to reference and invoke different types of commands through polymorphism.

 

ConcreteCommand:

  • Defines a binding between receiver and an action.
  • Implements the execute method by calling the corresponding method on the Receiver.

 

Receiver:

  • Has the actual implementation of actions that are to be carried out by the corresponding commands.

 

Client:

  • Has a reference to the Receiver and is responsible for instantiation of concrete commands.
  • Passes the receiver reference to a concrete command.

 

Invoker:

  • Calls the execute method on commands
  • May hold a collection of commands to be invoked at a later time.
  • This is a role, not a concrete class. A single class often fills both the client and the invoker role.

Example Uses

  • Decouple input from actions of in game actors, so that AI and player scripts can use the same actor control interface. A strategy game unit may present a method for movement, when given a move command with target coordinate. Both AI code and a mouse click on empty terrain could return an identical movement command. This command may then be forwarded to the unit, which has no knowledge of AI or input code.
  • Decouple input from actions of in game actors, so that an action could be rebound to different input during runtime.
  • Wrap time stamped method calls of AI in commands, so that the actions could be serialized. Serialized commands can be saved and loaded from disk to create replay of AI actions.
  • Wrap player actions in commands, which are kept in a collection. By implementing a reverse() method for every command and keeping a collection of latest player actions a undo functionality can be exposed to the player. 
;