I'm currently making an Entity System, using C++, and I've questioned myself about how I should interface some methods. Specifically in my Entity and ComponentManager classes.
The ComponentManager, manages all components for all entities in a specific world. It can add/remove/determine if a Component exists, etc. Now to retrieve a component, I was thinking of using a template function, that way it looks neater and it doesn't have as many casts (outside the method). But then I want quick access to components through an Entity object. e.g.
entity.getComponent<VelocityComponent>().x = 30;
(and yes I know an Entity is just an ID, that's why I have my components in the ComponentManager class, and each Entity is represented by an ID).
i.e.
ComponentManager's get function: (Components are stored as a 2d array, with the first element being the ID of an entity and the 2nd element being the ID of a component's class type)
template <typename Comp> // Comp = Component
Comp* getComponent(Entity& e)
{
return (Comp*)_components[e.getId()][Comp::GetClass().getId()];
}
And the Entity's get function:
template <typename Comp>
Comp* getComponent()
{
return getWorld().getComponentManager().getComponent<Comp>(*this);
}
So if I was to use template functions I would just have 2 duplicates of template functions. Now of course, this goes the same for adding components to an Entity. So basically, is it a good idea to wrap a template function with another template function?
I can, alternatively make the the methods in the Entity system templates and make the methods in the component managers non-template functions, i.e.
Component* ComponentManager::getComponent(Entity& e, const Component::Class& componentClassType) const
{
return _components[e.getId()][componentClassType.getId()];
}
I'm confused on what I should do.: