For fun I decided to try to make a simple entity component system. I have a list that contains all Components and I made a function called getPositionComponent that takes the entity ID and returns the position component related to that entity. All of the components I make are derived from the Components class.
struct Component
{
public:
std::string readableName;
Component::Component();
Component(std::string name);
virtual ~Component();
};
Position component
struct PositionComponent :
public Component
{
public:
PositionComponent(int x, int y);
~PositionComponent();
int x, y;
};
This is how getPositionComponent looks like
PositionComponent* ComponentManager::getPositionComponent(int entityID) {
int index = entitiesLookup_[entityID] + positionComponentsLookup_[entityID];
Component *comp = &components_[index];
PositionComponent* postionComponent = dynamic_cast<PositionComponent*>(comp);
if (postionComponent != nullptr)
return postionComponent;
return nullptr;
}
When I run this, it always returns a nullptr when I use dynamic_cast. While debbuging I can confirm that Component *comp = &components_[index]; returns the correct component type.
On a side note, I was reading one article where for an MMO they used SQL to store components. How slow/fast would sql be for pulling data locally from components in the game loop in a single player game?