Component based entity system implementation?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Component based entity system implementation?

Post 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.
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Component based entity system implementation?

Post 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/
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Component based entity system implementation?

Post 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.
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: Component based entity system implementation?

Post 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.
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Component based entity system implementation?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply