Imagine a platform game in which you can jump over obstacles when you press the space bar. In this scenario, every time you press that input key, you are asking the character on the screen to change states and perform a jump action. In code, we could implement a simple InputHandler, which would listen for a space bar input from the player, and when the player hits it, we would call CharacterController to trigger the jump action.
The following very simplified pseudo-code sums up what we have in mind:
using UnityEngine;
using System.Collections;
public class InputHandler : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("space"))
{
CharacterController.Jump();
}
}
}
As we can see, this approach can get the job done, but if we wanted to record, undo, or replay an input from the player at a later time, it could become complicated. However, the Command pattern permits us to decouple the object that invokes...