Jump to content
  • Advertisement
Sign in to follow this  
JuliusDegutis

Component-Entity-System implementation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

 

I'm trying to implement this systems, because inheritance has bored... I have done this in C# by finding code and changing for my needs. But on C++ I can't find any good example what I want.

 

I created basic Component class. Made some new class from it (SpriteComponent and MovementComponent). Now in Entity I want create list of components. But just don't know how to find it.

 

What I looking for is 3 methods and data structure for holding components .

  1. AddComponent(Component * component); would use it like player.AddComponent(new SpriteComponent(20,30));
  2. GetComponent(...); would use it like player.GetComponent<SpriteComponent>();
  3. RemoveComponent(...); would use it like  player.RemoveComponent<SpriteComponent>();

 

Could anybody say how to implement this thing?

 

Share this post


Link to post
Share on other sites
Advertisement

You can do this by storing your components in a std::vector or std::list

std::vector<Component*> m_components;
 
void add Component(Component* component)
{
    m_components.push_back(component);
}

 

For retreiving them your are going to have to look into templates in the manner you have given your usage for them or you change the way you store them and use a map<std::string, Component*> instead. Which would allow you to find components by name in the map with the find function that is defined for an std::map<T,S>.

Share this post


Link to post
Share on other sites

I think it gets contrived when you put the components into the entity. As soon as they are inside that array of pointers, the SpriteComponent looks like a generic Component and you then have to do weird things like testing all components for their exact type, using dynamic_cast or your own worse implementation of RTTI.

Thats why I favor turning the whole thing inside-out and putting components into the systems instead, like I described in the other thread: http://www.gamedev.net/topic/648465-csfml-ecs-how-to-deal-with-the-entities/

 

If you want to know more read this article series, its eye-opening! http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!