I wasn't sure how to word the title of this question, but here's my problem:
I am working on a game in C++ using the Entity Component System architecture. In this architecture, each game object is represented as a class:
class Object
A class is not much more than a container for components, which handle parts of the game object's logic.
class Component
Each component has an update function and event handlers for certain events. Each Event derives from a common base class, and each child object has it's own data fields to describe the Event. In my actual code, the base Event class has some attributes which all Events share.
class Event
{
};
class OnDeathEvent : public Event
{
public:
int getKillerID() const
{
retu 92341;
}
};
class OnLevelEnterEvent : public Event
{
public:
int getLevelEnteredID() const
{
retu 100;
}
};
Throughout the code, we send events to objects. Here's the main function I wrote to test my example code:
int main()
{
Object obj;
obj.sendEvent(OnLevelEnterEvent());
obj.sendEvent(OnDeathEvent());
std::cin.ignore();
retu 0;
}
When an Object receives an Event, it delegates responsibility for that Event to it's components.
for (auto& component : components)
{
component->handleEvent(e);
}
I want to use function overloading for each Event type. Event handling functions then become very clear in their purpose.
void handleEvent(const OnLevelEnterEvent& e)
But doing this requires that the Object class AND the Component base Class both declare all the overloads for every Event type. Obviously, this is something we want to avoid because we would have to change multiple classes to add a new Event type to our game. Here is the rest of the example code:
class Component
{
public:
virtual void handleEvent(const Event& e) {}
virtual void handleEvent(const OnDeathEvent& e) {};
virtual void handleEvent(const OnLevelEnterEvent& e) {};
};
class TestComponent : public Component
{
public:
virtual void handleEvent(const OnDeathEvent& e)
{
std::cout << "You died. Your killer was: " << e.getKillerID() << std::endl;
}
};
class AnotherComponent : public Component
{
public:
virtual void handleEvent(const OnLevelEnterEvent& e)
{
std::cout << "Level Entered with ID: " << e.getLevelEnteredID() << std::endl;
}
};
The object class:
class Object
{
public:
Object()
{
components.push_back(new TestComponent());
components.push_back(new AnotherComponent());
}
void sendEvent(const Event& e)
{
for (auto& component : components)
{
component->handleEvent(e);
}
}
void sendEvent(const OnDeathEvent& e)
{
for (auto& component : components)
{
component->handleEvent(e);
}
}
void sendEvent(const OnLevelEnterEvent& e)
{
for (auto& component : components)
{
component->handleEvent(e);
}
}
private:
std::vector<Component*> components;
};
We want to be able to add a new Event type by inheriting from the Event class, without requiring changes to other files or creating new files (other than the new Event class). What would be an elegant way to structure our code?
Any suggestions are welcome.
