# How to implement a Component-Based Entity system?

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

## Recommended Posts

I'm starting to work on a 2D RPG in Java using the libGDX API(if it affects anything at all). I've seen a lot of talk about "Component-Based Entity Systems" and don't really know a lot about how they work. I've done some research and have a basic knowledge on what they are but I still don't know how I would implement something like this. Can anyone give me some advice or point me in the right direction?

Thanks.

##### Share on other sites

Maybe this isn't a good explanation for your problem but have you taken a look at Unity (the game engine)?

They use component-based entities as well.

As for the programming part, there possibly are some engines that use this method that are open-source.

What would do is create a base class called, for example, BaseComponent and then create a lot of components that you want to use that use BaseComponent as their parent class. Then you'll need some kind of base class that resembles the basic of an Object to which you want to add components, just like in Unity. Let's call it GameObject.

Now you can create seperate objects that all use GameObject as their parent class. In this class you'll have an array of components. So, you can create a new component and afterwards add it to the array of components of a specific GameObject.

Say you added a component that holds the transformations of an object. You could, for example, scale the object by doing the following:

gameObject->GetComponent<TransformComponent>()->Scale(5.0f);


That would scale the gameObject 5 times it's normal size.

That's basically how it works.

##### Share on other sites

I suppose you don't figure out what a component based system is because - as I see it - "component based" is a hot buzzword and everyone on the internet feels completely entitled to write about them, even though they shouldn't.

I think the 1st thing is this article about Mick West. Main point here is the "parallelism" between the components as noted on the pictures.

This gamedev.net article is a bit minimalistic and thus good. Keep in mind that terminology is an issue, let alone model. Regardless of the post count about component systems some fundamental issues appear to go along undiscussed and likely unnoticed.

Take the example above. Component based systems take for granted components are orthogonal to each other so TransformComponent can be manipulated. I dare you to find a physics library allowing you to do that with no repercussions - the above operation is only valid for kinematic objects (in the case of Bullet, it would wreak havok even on kinematics).

In general, what I suggest is to read a minimal amount of things first. Iterate to better understand your problem and then refine your solution, using your data and your (now) deeper problem understanding.  In the end, details are the problem here.

##### Share on other sites
Take a look at this Framework. Its compatible with Java. I am also using it together with libgdx.

##### Share on other sites

I don't know if this helps you, but here's my implementation. I used C++ and the STL, and you can check it out in action on the first game I made with it. My implemented components are here, and the object spec files are here.

It was a little annoying to grab other components as well as limited in that I couldn't have separate update times (like render and take input every frame, but only move every 1/4 second etc.). It's also annoying to have to use the same template every time, but the advantages outweigh the disadvantages (for example, I will soon be able to prototype completely new objects via the CLI)

Edited by makuto

##### Share on other sites

What was the issue with rendering and input? Generally you want logic and rendering separate anyway, so I always ended up with separate update/render methods (or perhaps more accurately, separate render objects for everything but the GUI)

##### Share on other sites

Mainly just that I wanted alignment via grid in a roguelike style, and there's no reason to update logic every frame (because things only happen every 0.25sec). Now that I think about it, I could've updated everything every 0.25, and had 4 FPS, because it didn't really matter.

I'm trying to figure out how I want to separate the updating. Should I have two update functions & component lists, then two object manager updates that update each respective one? That would allow me to dynamically choose which update section each individual component can use, but it is a little complex.

Edited by makuto