# About my component system implementation

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I'm writing this post after my reply to the post "Shall entities know how to render itself?" I spoke of a component system that I use, and received a few pms from people curious about the system, and I promised I'd write it up, so here it is! I'll start by giving a brief overview of the system for people who are not familiar with it, and then I'll show how I implemented it. At the end of this post is a link to my source code - the way I'm doing it. So first, the intro. What is this? Essentially, the component system is a way of describing objects in a game engine, in a way that is reusable. It is similar to why we use inheritance, so we can extend. The component system works on the object level, rather than the class level. What I mean by this, is instead of extending functionality of an entire class, we can extend the functionality of a single object. This brings the benefits of very minimal bloat in places, and - in my opinion clarity. Here's a quick example of how it might work:
// First, we have to create the object.
ObjectID npc = ObjectManager.CreateObject("NPC #547");
ObjectManager.CreateComponent(npc, "/Common/Entity")
ObjectManager.CreateComponent(npc, "/Graphics/AnimatableMesh")
ObjectManager.CreateComponent(npc, "/Gameplay/Communicatable")
ObjectManager.CreateComponent(npc, "/Common/ScriptedInteraction")


About the example Ok, so that might not mean a lot to you, so I'll quickly go over the code presented above. The first thing that I do is create an "ObjectID." This is merily an index to the object, and ObjectID contains only 2 properties, ID and name. I request the object manager create an object, called "NPC #547" and it returns a unique id. The name part is just for scripts to find objects, btw. The lines after that are me constructing the object. I call the CreateComponent method of my object manager, tell it the object to add the component to, and the name of the component. To quickly sumarise the components above, entity provides the location of the entity, and its orientation. AnimatableMesh does, well, what it says on the tin, Communicatable means that the player can communicate with the object, and scripted interaction means that any interaction will be handled via a script. I've purposly left a lot of the details out in that example like what script to use, setting the entity properties, and stuff like that. I left them out simply for clarity. Components Every component in my system runs of an abstract class that is:
public abstract class Component
{
protected ObjectManager objMan;

private ObjectID obj;
public ObjectID Object
{
get { return obj; }
}

public virtual void Initialise(ObjectID objectId, ObjectManager manager)
{
...
}

public virtual MessageResult HandleMessage(Message m)
{
...
}
}



##### Share on other sites
Wow. Thanks. I can't wait to try this out myself.

##### Share on other sites
I am not able to get it to compile just yet, but I do find looking through your source code refreshing.

The way I am coding my engine right now I absolutely hate, and the way you seem to have done it is the way I previously wanted to do mine.

Thank you very much for posting this code.

##### Share on other sites
Glad to see it's helping :)