1

So today I am learning and implementing Command Patterns for handling input and movement for objects.

So my question is:

  1. Am I getting the implementation of Command Patterns right or do I need to modify it? If so, can somebody give me a little example on to improve it.
  2. I know that it improves code reusability. But what difference does it make when I just use a simple MovementScript.cs to my game object component? Wouldn't it just be the same and take less time to write rather than making a whole Command Pattern?

The one I attached to the gameobject is only the InputHandler. Here's my code which involves moving an object:

This is my Input Handler or as far as I know as The Client

public class InputHandler : MonoBehaviour
{
GameObject theObject;
public Command buttonA, buttonD;
public float acceleration, maxSpeed;

Movement moves;

void Awake()
{
    theObject = gameObject;
    moves = new Movement(theObject, acceleration, maxSpeed);
}

void Start()
{
    buttonA = new MoveLeft(moves);
    buttonD = new MoveRight(moves);
}

void Update()
{
    HandleInput();
}

public void HandleInput()
{
    if (Input.GetKey(KeyCode.A))
    {
        buttonA.Execute();
    }
    else if (Input.GetKey(KeyCode.D))
    {
        buttonD.Execute();
    }
}
}

The Command abstract class

public abstract class Command
{

//The Receiver of the command..
protected IReceiver receiver = null;

public Command(IReceiver receiver)
{
    this.receiver = receiver;
}

public abstract void Execute();
}

The Receiver class (where I implemented the logic, which is the movement)

public class Movement : IReceiver
{
public ACTION_LIST currentMoves;
private GameObject theObject;
private float acceleration;
private float maxspeed;

public Movement(GameObject theObject, float acceleration, float maxspeed)
{
    this.theObject = theObject;
    this.acceleration = acceleration;
    this.maxspeed = maxspeed;
}

public void Action(ACTION_LIST moves)
{
    if (moves == ACTION_LIST.MOVERIGHT)
        MoveRight(theObject);
    else if (moves == ACTION_LIST.MOVELEFT)
        MoveLeft(theObject);
}

public void MoveRight(GameObject obj)
{
    obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(acceleration, obj.GetComponent<Rigidbody2D>().velocity.y));
}

public void MoveLeft(GameObject obj)
{
    obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(-acceleration, obj.GetComponent<Rigidbody2D>().velocity.y));
}

}

Interface of receiver, to make things easier..

public enum ACTION_LIST
{
    MOVERIGHT,
    MOVELEFT
}

public interface IReceiver
{
    void Action(ACTION_LIST moves);
}

The concrete command. I only posted 1 of the movements..

public class MoveRight : Command
{
public MoveRight(IReceiver receiver):base(receiver)
{

}

public override void Execute()
{
    receiver.Action(ACTION_LIST.MOVERIGHT);
}
}
Bart
  • 19,692
  • 7
  • 68
  • 77
Deni Cho
  • 19
  • 3
  • 3
    This might also be a good question for [code-review](http://codereview.stackexchange.com/) – MX D Jul 25 '16 at 14:14
  • Except that everyone there is naive and nobody understands ECS systems :) Embarrassingly, on code review people would start trying to encapsulate it (as Deni has), not realizing it's *a completely trivial operation, already built-in to Unity*. – Fattie Jul 25 '16 at 17:36
  • Full explanation of how you do this in Unity: fortunately it's easy http://stackoverflow.com/a/35891919/294884 – Fattie Nov 21 '16 at 12:08
  • Possible duplicate of [Unity game manager. Script works only one time](http://stackoverflow.com/questions/35890932/unity-game-manager-script-works-only-one-time) – Fattie Nov 21 '16 at 12:08
  • "But what difference does it make when I just use a simple MovementScript to my game object component?" Correct, it makes absolutely no difference. It's program for reusability but: when you are talking about an atomic operation, there is nothing to "conceptualize out"! Unity have already done the work: it is already totally reusable. It's 1 line of code. "Wouldn't it just be the same and take less time to write rather than making a whole Command Pattern?" Note that it is actually *not possible* to make a "command pattern" in Unity. – Fattie Nov 21 '16 at 12:10

1 Answers1

2

I disagree with Joe Blow and others saying that Unity is a bunch of scripts and not OOP. I use main script with single entry point and create all objects and components dynamically. I even use interfaces, mock and unit tests.

So, using Command pattern is ok. But I don't see a reason to use it in your case. Command pattern could be very handy in case when you need a stack of commands to be able Do() and Undo() your commands (Editor, Strategy game). Please read more here: http://gameprogrammingpatterns.com/command.html

Dzianis Yafimau
  • 2,034
  • 1
  • 27
  • 38
  • 1
    Unity ***IS NOT*** OO. It does ***not even have inheritance***, for goodness sake. It is ***exactly like using Photoshop***. Certainly, *the **language** currently used to **write** Component for Unity, happens to be an OO language, so you must be absolutely expert at OO in that sense*. (This could change tomorrow - they could change to using Lisp or something.) In the actual example at hand, it's just totally inconceivable you would do anything other than use the one line of code Unity makes available for the purpose. – Fattie Jul 27 '16 at 16:04