Using Commands.

Command Coding Pattern

The Command pattern in coding is very useful when it comes to linking player input to different functions. This pattern is used often in larger programs as it helps keep the code looking clean, organised and professional.

Using the Command pattern allows you to create a sort of interface between the command in your code and whatever you have calling it. It is also super useful for making commands easy to reassign to other areas in your program. A great example of this can be seen when reassigning player controls to work with any character within the game and even have an AI within the game use the same commands to operate that are used on the player. By changing the object/character that we are executing the commands on you are able to use the same commands that were being used to control the player. By using this same command pattern we are able to interface between the AI engine and the actors in the scene. From here you are able to plug in different modules for different AI actors to achieve different performance based on what you want different AI characters to accomplish. An article that goes further into the workings of command patterns and different places they can be applied can be found HERE. I found this to be very helpful in my study of Command Patterns.

My Experiments With Command Patterns

My workings with the Command Design Patterns are very limited but I have been trying to learn how to implement them a little more through little experiment programs of my own. What I wanted to accomplish with with this little experiment was a sort of weapon cycle system. This was just an idea that seemed like something I wouldn’t mind trying to create using the command pattern so I gave it a go.

First I created an empty game object in unity where I would place all of my scripts (for what I am doing this works perfectly). I then defined a base class within a script called ‘BaseCommand’ that represents a triggerable game command. This is a base class that simply defines Execute as being non existent. This is a very small script that looks like this:

public abstract class BaseCommand {
 public abstract void Execute ();
}

 

Next I created a DefaultCommands script that contained and defined all the different Commands that I would be using. Looked a little something like this:

public class Attack : BaseCommand {
 public PlayerWeapons playerWeapons;
 public Attack (PlayerWeapons _playerWeapons) {
 playerWeapons = _playerWeapons;
 }
 public override void Execute() {
 Debug.Log(playerWeapons.currentWeapon.ToString() + " Fired!");
 }
}

public class ChangeWeapon : BaseCommand {
 public PlayerWeapons playerWeapons;
 public ChangeWeapon(PlayerWeapons _playerWeapons) {
 playerWeapons = _playerWeapons;
 }
 public override void Execute() {
 Debug.Log("Changed Weapon");
 playerWeapons.currentWeapon++;
 if (playerWeapons.currentWeapon >= playerWeapons.weapons.Length) {
 playerWeapons.currentWeapon = 0;
  }
 }
}

 

These are both getting something called PlayerWeapons which is another script that contains all the player weapons. Currently all the PlayerWeapons script is doing is creating an int array that is filled through the inspector called weapons and an empty int called currentWeapon. As you can see in the code above, both of these variables are being used and changed to allow the player to cycle through any given amount of weapons and attack with each one taken into account. Both of these commands also take a parameter called “_playerWeapons”. This will be shown latter but it is basically hands in the class “PlayerWeapons”. At the moment this code is only creating strings that tell you if a weapon has been “changed”, when a weapon has been “fired” and what weapon number has been fired.

Now that I have the code that I want to run, I have to have a way to run it. To do this, I created a script called InputManager that manages input from the player. In total there are 2 Commands that are being managed and they are structured exactly the same but have different names. This code looks like this:

 if (Input.GetKeyDown(KeyCode.Return))
 {
 if(Attack != null)
 {
 Attack.Execute();
 }
 }

This code gets the input from the player and runs “execute” on the “Attack”s definition of “execute” when it receives the key input “Return” (We saw how the code in the Attack class has it’s own definition of Execute and how it overrides the original Execute with the code “public override void Execute() { }“). Changing what key you would like to execute this code is as easy as changing “Return” to any key you like. You can also get another input that is not related to player input at all. Say you wanted something like an AI to use this code (referring back to previous example), this is where you would make that happen.

Now moving on to how we assign these commands. At the moment I have told my program how I want to activate/call certain commands and what I want them to do but these 2 parts have no communication with each other. Sure I can hit the “Return” key and it will run the ‘If’ statement (If (Attack != null)) but it will return false and the line ‘Attack.Execute();’ will not run as it currently doesn’t know it exists and is equal to ‘null’.

 public InputManager inputManagerRef;
 public PlayerWeapons playerWeaponsRef;
// Use this for initialization
 void Start ()
 {
 inputManagerRef.Attack = new Attack (playerWeaponsRef);
 inputManagerRef.ChangeWeapon = new ChangeWeapon (playerWeaponsRef);
 }

 

This code gets both the InputManager and PlayerWeapons scripts and uses inputManagerRef and playerWeaponsRef to reference to them inside the current class. Getting them this way will require you to link both these scripts through the unity client (You CAN do this through other means. This is just how I have done it). It then takes both Attack and ChangeWeapon from InputManager and loads them with the Commands that I wrote in the DefaultCommands script. It also passes in the PlayerWeaponsRef as the parameter for both the classes, giving them access to the information needed from the PlayerWeapons Class (this was looked at before when talking about the the DefaultCommands script).

With all of this put together, I have successfully setup a functional Command structure for myself to play around with and improve upon. As I have mentioned before, by using this structure you am able to accomplish many more complex tasks than just what I have shown here. This is just what I have learnt during my recent research. I plan to improve on my understanding of the Command Pattern and the frequency of which I apply it to my own practice in the future.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s