Sign in to follow this  
Nanook

Composition implementation

Recommended Posts

I'm implementing enteties in my game using components. I'm strugling to decide how to implement it.

I have a CollisionComponent and a ContactGenerator.. in the ContactGenerator I need all the CollisionComponents of all the entities in my game.

First I have been thinking of implementing my Enteties something like this;

[source]
class Entity
{
public:
Entity(CollisionComponent *colcomp
SomeOtherComponent *othercomp)
: m_pcolcomp(colcomp), m_othercomp(othercomp)
{}

void Update()
{
//Update all components that are not null
}

CollisionComponent * GetCollisionComp()
{
return m_pcolcomp;
}
private:
CollisionComponent *m_pcolcomp;
SomeOtherComponent *m_pothercomp
}
[/source]

But doing it like this when I'm calling .GenerateContactSets() on the ContactGenerator I would have to either pass inn a vector of entities or build a vector of CollisionComponent's before I call it and pass that in.. With passing in CollisionComponents instead of enteties the collision stuff doesn't have anything to do with the enteties.. I've seperated all the collision stuff into a library so this is what I want..

Instead though I have been thinking of having maps for each of the components.. Then I can pass in the map with the CollisionComponent's into the .GenerateContactSets() function instead of building a vector first..
Something like this:

[source]
struct World
{
std::map<std::string, CollisionComponent> collisionComponents;
std::map<std::string, otherComponent> otherComponents;
}
[/source]

Are there any scenarios I'm not thinking about where this is a bad idea? Would all the hasing into the maps be a big overhead compared to only storing pointers? What do you think of this design?

Share this post


Link to post
Share on other sites
Caching pointers to subcomponents is fairly common, although you probably don't want a std::map<std::string, Foo> because that will do a string compare for every node during searching, which will get slow. Numeric IDs are a pretty common solution to this issue.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this