I've been doing research on component-based game engines and would like to use that model for future game projects. From what I gather, objects in a component-based system are just collections of attributes and behaviors. The examples I've seen have a high-level interface such as Attribute<T> *Entity::getAttribute<T>(int id)
but discussions about those systems often talk about performance problems. It seems like this could be implemented with greater efficiency in C++ using multiple virtual inheritance since the compiler could inline attribute accesses and behavior method calls:
// Layer 1: Base
struct Entity {
virtual ~Entity() {}
virtual void draw() {}
virtual void update(float seconds) {}
};
// Layer 2: Attributes each extend from Entity
struct Position : virtual Entity { Vector3 pos; };
struct Velocity : virtual Entity { Vector3 vel; };
struct Health : virtual Entity { int health; };
struct Target : virtual Entity { Position *target; };
// Layer 3: Behaviors extend from the attributes they need
struct Renders : virtual Position {
Model model;
void draw() { model.draw(); }
};
struct Moves : virtual Position, virtual Velocity {
void update(float seconds) { pos += vel * seconds; }
};
struct Seeks : virtual Moves, virtual Target {
void update(float seconds) {
if (target) vel = (target->pos - pos).unit();
Moves::update(seconds);
}
};
// Layer 4: Definitions extend from the behaviors they need
struct Rocket : virtual Health, virtual Renders, virtual Seeks {};
However, many component-based engine resources discount multiple inheritance without really saying why. I'm sure I'm missing something, but what are the disadvantages of using multiple inheritance like this over lists of components? I know calls to base classes when using multiple inheritance incur an additional constant offset, but that seems better than going through a run-time map for each attribute access. The only disadvantage that I can think of is not being able to swap out components at run-time.
Resources: