• Advertisement
Sign in to follow this  

Composition implementation

This topic is 2323 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement