Design Patterns – The Command Pattern

Again, I am not gonna bother trying to find relevant images for this stuff, so have a nice picture of Wipeout Omega Collection that came out last week. Wipeout is my favourite Anti-Grav racer, sorry F-zero, I grew up with the PlayStation ūüôā .

Anyway, lets have a look at the Command Pattern. I have been reading up on the Command Pattern recently, and it seems pretty popular with it being Robert Nystrom’s favorite pattern! (He is the author of Game Programming Patterns. It is a good book, I would recommend it! He also has it all up for free on his website).

Unfortunately, as Mr Nystrom says, the Command pattern as an annoyingly obtuse description:

“Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”

Seriously, it would be nice not to here a technical definition of something in a language other than nerd once in a while. In English, Commands are an object-oriented replacement for callbacks. It allows you to encapsulate actions into objects with the main objective being to provide the means to decouple the client from receiver.

Does that kind of make sense? OK, well either way let’s look at an example or two.

In the real world, say you were at Maccy D’s after a night out. You want a large portion of C-Nugz with a Cheezy B for the road (or a Large Chicken Nugget meal with an extra cheeseburger. They are only like an extra quid, so why the hell not?). Let’s say it is old school maccys where there is someone taking your order. You (the Client) ask the guy (the Invoker) for your meal of goodness (the Command). The guy plugs that in and a request is made to the Chef(the Receiver) to create it. Do you see what is going on? The Client gives an Invoker a Command that is then past on to the Reciever that actually knows how to deal with the Command.

Let’s look at it in more game dev terminology. I am going to write some C#, because C# is awesome (C++ is alright too, but C# is easier to just get stuff done ūüėČ )

Let’s say I am making that really popular Side Scrolling Shooter on Vita known as Storm Ship Shiro. We will likely want to put in a way to remap the controls (something I admittedly didn’t do back in the day…). First I am going to make an abstract class called Command.

public abstract class Command
{
    public abstract void Execute()
}

The main part of the game I may want to remap is Firing the main Weapon. Firing the Weapon is a Command so what do you think I am gonna do? Make a class that inherits from this Command.

public sealed class FireWeapon : Command
{
    public override void Execute()
    {
        //Fire logic goes here
    }
}

Cool so we have our command, what is our Client. Well in this case that is more “conceptual”. You could say our player is the Client. Our Invoker however could be our Input System that has the following code:

Command onButtonA;
public void SetButtonACommand(Command command)
{
    onButtonA = command;
}
//A load of code...

if (Input.GetButtonDown(Button.A))
{
    onButtonA.Execute();
}

If we plug in the following:

InputManager.SetButtonACommand(_playerShip.FireWeapon);

Then we have mapped that to Button A.

In this case the Client is the player, the Invoker is the Input System, Firing the Weapon is the Command and the Reciever is the Player’s ship.

If we check our simplified definition:

“Allows you to encapsulate¬†actions in objects with the key idea being to provide the means to decouple client from reciever”

That is exactly what this Input system is doing.

Cool huh?

There are many other examples of how the Command pattern is used, one is Undoing and Redoing actions. Say you had a Tactics style game where you could see the outcome of your move. You move your character and then tell him to attack. You didn’t like that last action you performed. So you decided to return to just the move and then use an Item. The client here is still player but the Invoker is a core game system that has a load of commands for every action a character can do. The receiver¬†then is the character themselves.

The command pattern is pretty cool, have a look at it!

Share

Design Patterns – Intro

Image result for persona 5

As usual, I couldn’t find a decent picture, so I just put up one of Persona 5. I am still playing it, it is really good. Pick it up!

Recently I have been going through all my knowledge and figuring out where my core knowledge gaps are. After doing a few tests, etc, I found one of these was design patterns. I have used them a lot in the past, but there are still a few that I am not super comfortable with, so as with most things, I thought I would revise them by doing blog posts about them. Makes sense right?

Firstly, why are design patterns important?

I have worked with a number of code bases, both good and bad. Some code is built in an extremely nice way, other bits are hacked together willy nilly just writing code and not really thinking about the architecture to solve the problem. I myself have done both at some point or another, for a variety of reasons that I may go into in a later post. Design patterns CAN help with this, but there are some caveats.

Let me go on a bit of a tangent a bit. In 2017, games are often not a fire and forget bit of software. They are living breathing things which we can later update and make better. We have platforms like Steam, PS4, Xbox One and Switch which allow us to patch our games unlike the days of PS1 and PS2. We also have games that require lots of Live Operations and will require us to revisit code to add features or extend existing ones. We will essentially need to make changes and design patterns are one way that can kind of help us write code in anticipation that we are one day going to have to make these changes. They are no silver bullet, but they can help towards this.

But hang on, why is that the case? Well let’s look at the definition or what a design pattern is.

According to Wikipedia – “a software design pattern is a general reusable solution to a commonly occurring problem within a give context in software design.”

Let’s say you get to a code base ¬†and you need to make a change to a feature that you are not super familiar with. This feature is meaty, but luckily as you peruse through it you can see some familiar patterns and the classes, etc seem to be named in familiar ways. You realize that someone has used a design pattern and you can use your knowledge of the design pattern to get stuck in. The amount of time taken in understanding the code has decreased and the amount of time needed to make your change. This is because they are defined as reusable solutions to commonly occurring¬†problems. Yes you will still need to learn what the code is actually doing, but¬†as it is a solution to a problem that has cropped up before, their usage is communicating to you what the goal of this system was and it’s intention. Similarly, as it is a familiar solution to a commonly solved problem, the code is likely to be more readable and thus also easier to maintain, which is good as someone else comes back and revisits it again.

You have been given a feature on the game to do. It is pretty big and you need to architect a system. You come up with a system that fits within a design pattern. By doing this, you can easily explain to other developers how exactly you are going to implement a feature. You can talk about how you are going to use a Factory to handle all your object creation, or an Observer to handle when to trigger an event. It gives developers a common vocabulary to talk about and can be used as a good starting point in a discussion whether or not that it is a good way of implementing the solution to the problem you are trying to solve.

Here is a final example. Say you are writing a feature, and it sounds very similar to something that already exists. You look up the code for this old feature and find the underlying system is written in a nice reusable manner as it followed a specific design pattern. This is awesome as it means you won;t have to write a tonne of common code to solve your issue.

So let’s look at the key points at why Design Patterns are important:

  • They make code more¬†readable
  • They make code easier to maintain
  • They can make it easier to communicate your intentions to other developers
  • They allow for code re-use and less code

Sounds great, right. There are a fair few good reasons why you should use a design pattern, but there are caveats. Like I mentioned briefly before, they are not the silver bullet. And can cause problems.

  • They can make the code more complex – some of them patterns use a lot of indirection and less experienced members of your team may have a harder time following them
  • You need to know when to use them –¬†There are always dangers of using the wrong pattern and also using them just for the sake of it. Regardless of how senior you are, when developing a big feature and want to use a design pattern, chat it through with another developer. Otherwise it will make life harder rather than easier
  • Some can hurt game performance –¬†patterns can make your code more flexible and easier to reuse, but this can in some cases come with a cost to performance. They can rely on features that have a greater runtime cost. There needs to be a balance here. Although you are writing more flexible and potentially easier to understand code,a re you shooting yourself in the foot when it comes to performance?
  • They can be abused –¬†Google the Singleton pattern and you will find a lot of discussion around its abuse.
  • You can code yourself into a corner –¬†This is a very specific case I have found when it comes to dealing with live games. Originally, you used a certain design pattern to create a feature based on the design. However the game is out in the wild and analytics are revealing that players are having a hard time with it, don’t enjoy it or it just doesn’t work as a concept. The game is a living breathing thing and you have to adapt it. The change you have been given is not that big conceptually, so you go to the code base. However, because you built the code in such a specific way, it is actually going to take a long time to unpick parts of it and put in this change. You essentially coded yourself into a corner.

So yeah, Pros and Cons. The main points here are:

  • Design patterns are powerful and can help make your code better,¬†when thinking about using¬†them, research them and discuss your solution with others to evaluate the pros and cons.
  • Don’t be the “Small Boy with a design¬†pattern” and use them for the sake of it

Awesome! Next we will be looking at some of the Command Pattern!

Share

Unreal Engine and Space JRPG

Image result for chaos rings 3

This year I wanted to learn something new. I also wanted to work on a hobby project that other people cared about and get excited about, not just me, so I have been cocnepting a new idea for a “light” sapce jrpg. I think the idea is pretty cool… the amount of content required for it is quite scary, but you know CROSS THAT BRIDGE WHEN I COME TO IT I GUESS :/. Either way, I am using it as an opportunity to revise all my C++ and learn Unreal Engine. I am pretty good at the C# business and Unity, considering I have been using them for the best part of 6 years (commercially and not), and also learnt unity before it was cool.

Although JRPGs sound hard, the actual code and systems are not too bad, it is mostly balancing and creating nice tools to deal with it. Unreal Engine is a tool that is well used on a variety of sexy looking games and it is a gap ion my knowledge that I want to fill. I don’t want to say too much about what I am making yet until I have some viable gameplay, but as soon as I do you will hear it. I will say platform wise I am aiming at mobile as I reckon mobile premium is on the way back… I may be wrong, but hey! Also go p[lay Chaos Rings 3 on mobile, it is good!

Share