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

Component based entity system implementation?

Postby 0x0000000 on Sun Sep 14, 2014 4:58 am

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.
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 42
Joined: Tue Feb 12, 2013 1:46 am
Location: Memory Address 0x0000000 (in UTF-9 notation)

Re: Component based entity system implementation?

Postby dandymcgee on Sun Sep 14, 2014 10:40 am

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/evolve-your-heirachy/
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Component based entity system implementation?

Postby X Abstract X on Sun Sep 14, 2014 6:06 pm

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.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
 
Posts: 178
Joined: Thu Feb 11, 2010 11:46 pm
Location: Ottawa, Canada

Re: Component based entity system implementation?

Postby 0x0000000 on Sun Sep 14, 2014 6:57 pm

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.
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 42
Joined: Tue Feb 12, 2013 1:46 am
Location: Memory Address 0x0000000 (in UTF-9 notation)

Re: Component based entity system implementation?

Postby dandymcgee on Mon Sep 15, 2014 3:21 pm

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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire


Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron