I’ve been meaning to blog something for quite some time now, but I haven’t really had the time and when I had the time I was going through some personal stuff. Basically my wife left me right before Christmas, it hasn’t been easy since then, but now I’m going to try and pick up the slack. Now that’s not what this blog is going to be about however, I want to write about a pattern that we are using in the Advanced c++ class.

Components

I love components, I just gotta say that. Now simply put a component system is all about taking away all the details that you normally want your gameobject class to have, I don’t mean everything of course, but for instance if the game object has a visual representation you make that as a component. Inputs, movement, health, collision etc. What you do is make a basic interface component class. I called it “InterfaceComponent”, reason being that when I used the class wizard in VS2010 it said the word component was a reserved class.

The InterfaceComponent class is very simple. It’s basically an pure virtual class with certain methods like Init, Update, Clean and a way to identify the component in question. Reason why you want to identify the components is that when you link it with a game object, because the GameObject class need to contain an method for attaching the interface. Instead of making the GameObject a pure virtual class as I usually do, I basically ripped off the teachers code, however what it does is to use a Template method to insert the right type of component into the gameobject class. All the other components inherit from the InterfaceComponent class.

So why use components, you need to write code to attach the gameobject into the component and you need to initialize the gameobject into the component, so why do it like that? Simple answer to that is that you make components because once the system is done, you can easily add more components later on. The longer answer is that, let’s say you have 10 gameobjects that need a new component, you create the component once and just attach the new component to the system. Also it’s makes the code modular so more people can work on the code at the same time. If the game we were creating needed a bigger team, each component can be distributed among the people working on it. Besides, the code is reusable later, which is another big plus. Generic code is what we strive to write and the component system is more or less the first pattern that I’ve seen that does just that. Of course not all aspects of the code is reusable, however you don’t need to modify the code much in order to reuse it for another game or even another program.

I probably isn’t making much sense for those who doesn’t program anything, however you can compare components to a car. In order to drive the car you need wheels, engine, steering wheel, gas, gas tank and someplace to sit. Of course you need more, however what you do is that you define the car as an empty shell. Then you add via code, the small parts that you build the car with and once you have that, you can easily change the parameters for the car. How fast, accelleration, because you have easy access to all of that data. Add serialization, or reading data from a file to automate the process and you can with the use of that create cars for all purposes. Of course you can do that without components as well, but one reason for having components is that you can easily remove and add components without changing anything in the object you want to add the stuff to. You don’t have to add variables, you just create the component, attach it to the game object and the update of that object happens automatically.

The tricky part with components however is to know which component you want to have access to the other component. As an example, I have a SpriteComponent and a PositionComponent. In the SpriteComponent I get the PositionComponent from the gameobject. The PositionComponent in turn updates the sprite, where it should be positioned. Then I have a movement component that contains the InputComponent and also the PositionComponent, reason is that you want to have movement update the position of the sprite, which it automatically gets from the PositionComponent. So having the different components talking to each other is also important, that’s why you initialize the components with the gameobject, because that’s where you store the component you add to the game object.

I know it’s useless to write about code without practical examples. I tried however to put some code in here but the formatting didn’t look any good so I removed it. That said, components, good system, use it! Provided some links below which has some examples, and to learn more about this pattern. Or ask below and I’ll see if I can answer the questions.
Links:
http://gameprogrammingpatterns.com/component.html
http://sourcemaking.com/design_patterns/composite