Page 1 of 1

Component based entity system implementation?

Posted: Sun Sep 14, 2014 3:58 am
by 0x0000000
First of all, congratulations ES team for realizing your dream. You guys are the arguably the biggest inspiration for me and countless others. Do not ever give up.

Ok, so I've been roaming around the internet looking for actual code implementations (C++) of component based entity systems and so far I have found none. I understand the concept; you can attach components to a single 'Entity' class, and as soon as you do that a system will add the entity to a list of entities to be updated. For example you have a 'Render' component. You would do something like this:
// UNTESTED PSUEDO-C++ CODE
RenderComponentSystem rcs;
Entity e;
/*
    This will somehow add the RenderComponent to a vector(?) of pointers
    to the base 'Component' class.  It will also add the entity to the
    RenderComponentSystem to be updated accordingly.
 */
e.attachComponent(static_cast<Component*>(new RenderComponent()), static_cast<ComponentSystem*>(&rcs));
// But how would you access that component?
// e.components(RENDER_COMPONENT)->blah() ?
Then you would have the issue of removing components. Perhaps the entity could store a pointer to the ComponentSystem for each Component, then update the system accordingly?

Any help is greatly appreciated.

Re: Component based entity system implementation?

Posted: Sun Sep 14, 2014 9:40 am
by dandymcgee
I wrote this a while ago, and I make no claims of it being the best approach or even a good one, but it might give you some ideas.
https://github.com/dbechrd/CompositionDemo

Based on my interpretation of this article: http://cowboyprogramming.com/2007/01/05 ... -heirachy/

Re: Component based entity system implementation?

Posted: Sun Sep 14, 2014 5:06 pm
by X Abstract X
I don't personally use an entity based system but here's the first approach that comes to mind.

- Store a string containing the name of the component in each component class (or virtual method you can overload to return a string)
- Instead of using a vector in your Entity class to hold components, use an unordered_map<string, vector<Component*>>

You could then grab all the renderable components in a single object for example:

std::vector<Component*>& renderableComponents = entity.getComponents(RenderableComponent::name());

Of course you'll have to use a dynamic cast to cast from a list of Components to RenderableComponents. If you don't need multiple components of the same type in the same object you can scratch the vector.

Re: Component based entity system implementation?

Posted: Sun Sep 14, 2014 5:57 pm
by 0x0000000
Thank you guys. Dan's implementation uses global variables and basically merges the job of a component system into the component class itself. I will have to study this further.

Re: Component based entity system implementation?

Posted: Mon Sep 15, 2014 2:21 pm
by dandymcgee
0x0000000 wrote:Thank you guys. Dan's implementation uses global variables and basically merges the job of a component system into the component class itself. I will have to study this further.
I don't recall there being any global variables. There are static collections composed within each component class. While one could argue these are globally accessible in any file that includes the respective header, they don't pollute the global namespace.

I'm actually not sure if public static class members are considered bad practice in C++.

Edit: Added link to article which inspired my design to my first post.