Component Based Architectures

Started by
1 comment, last by Nairb 15 years, 11 months ago
Inspirations for this post: Component Based Game Architectures - GDNet Post Outboard component-based entity system architecture - GDNet post Object Oriented Game Programming: The Behavior System 301: Introduction to Dungeon Siege Architecture Various Bilas presentations Some stuff written by Jeremy Chatalaine So, now to begin with the actual post. :-) I'm toying around with writing a purely component-based architecture, perhaps making something crazy data driven, and I was wondering what other people were doing here. I haven't read the GPGems articles on the topic, so I'm probably missing some details, but I'm grabbing everything I can find online. My knee-jerk idea was to create one master "GameObject" class which consisted of a list (or map, more technically) of components and never subclass that, simply define various schemas via XML or some other mechanism. Communication between components would either be done via a signal/slot mechanism or by providing a QueryInterface in the GameObject class such that a component could search for another component if it wanted that information. Signal/slot seems cleaner design-wise, but QueryInterface seems more compact. For instance, if something needed a Position component (hypothetical example - something so granular may not be a good candidate for its own component) to do its rendering at the right location, with the signal/slot mechanism the new position would have to be stored until needed, whereas with the QueryInterface it could just be used and discarded on demand. That was the knee-jerk reaction, of course, and it left me with all sorts of questions about how certain things would be handled. For instance, what happens when game objects need to know about each other (when pathfinding, for instance)? It seems like the common way is to have some master database and run queries on that database to find entities with the appropriate components? Further, what do I do about entities that should be added to the scene graph versus those that shouldn't? Upon creation of the entity, should the scene graph check its components and add it as necessary? I think I'm missing some key piece here. What about entity-specific behaviors? A person with a bow attacks differently than a person with a sword or gun, hence a general 'Attack' component doesn't seem practical. Is this where different kinds of Attack components (Attack_Sword, Attack_Gun) come into play, or is this where subclassing becomes the only pragmatic possibility? So it seems like having one master GameObject class is a little impractical, and that I'll be forced to throw in game-specific code, which is not necessarily a bad thing of course. I wonder about things like managing a bunch of interacting entities (for instance, parts of a window in a GUI). It seems like if I fit some scripting solution in, game-specific code would turn into scripted code, which may be ideal. So, again, I was wondering how other people were implementing their solutions. I realize right now that I'm trying to treat component-based design as a golden hammer or magic bullet, so where are you guys drawing the line? Or are you even using this at all? Cheers, --Brian
Advertisement
Quote:That was the knee-jerk reaction, of course, and it left me with all sorts of questions about how certain things would be handled. For instance, what happens when game objects need to know about each other (when pathfinding, for instance)? It seems like the common way is to have some master database and run queries on that database to find entities with the appropriate components? Further, what do I do about entities that should be added to the scene graph versus those that shouldn't? Upon creation of the entity, should the scene graph check its components and add it as necessary? I think I'm missing some key piece here.
I've laid out my thoughts on this in a couple of the threads you linked, so I'll abstain from these questions.

Quote:What about entity-specific behaviors? A person with a bow attacks differently than a person with a sword or gun, hence a general 'Attack' component doesn't seem practical. Is this where different kinds of Attack components (Attack_Sword, Attack_Gun) come into play, or is this where subclassing becomes the only pragmatic possibility?

Compare these two situations:
class FooBase {public:  virtual int exec() = 0;};class FooOne : public FooBase{public:  virtual int exec() { return 1; }};class FooTwo : public FooBase{public:  virtual int exec() { return 1; }};

class Foo {public:  Foo(int i) { m_i = i; }  int exec() { return m_i; }private:  int m_i;};

It's pretty clear which one of these approaches is clear and elegant, and which one is rigid and overly verbose. But the situation can go the other way:

class FooBase {public:  virtual int exec(int a, int b) = 0;};class FooAdd : public FooBase{public:  virtual int exec(int a, int b) { return a+b; }};class FooSub : public FooBase{public:  virtual int exec(int a, int b) { return a-b; }};

enum Operation { OPERATION_ADD, OPERATION_SUB };class Foo {public:  Foo(Operation o) { m_o = o; }  int exec() {     switch(m_o) {    case OPERATION_ADD: return a+b;    case OPERATION_SUB: return a-b;    default: assert(false);  }private:  Operation m_o;};

Here, denying the fundamental difference between the two operations leads to less readable, less trackable code. So there's no easy answers, except "try it out, and if it doesn't work out, try it the other way". As you get more accomplished, you'll learn to get it right the first time most of the time, but at least for now you'll get it right the second time.

One dimension, however, that does have a simple answer, is that this sort of subclassing should be confined as tightly as possible. See the strategy pattern for more details on this.
Quote:Original post by Sneftel
I've laid out my thoughts on this in a couple of the threads you linked, so I'll abstain from these questions.


Ah, so you do. Very first post in that second link, as it turns out. I think I managed to interpret a large portion of the discussion as pertaining to inter-component communication, and by the time I'd hit the end I'd forgotten all about that first bit.

This topic is closed to new replies.

Advertisement