Need some advice on a Component-Based Object System using the Observer Pattern

Started by
12 comments, last by Sean_Seanston 13 years, 4 months ago
Quote:Original post by Trefall
Components are meant to be modules of logic that add a piece of behavior to their entity. If you come in the situation where you have to have this component and those other three components to add that component, you're in trouble, because you're bound to make a mistake, or end up with large spreadsheets that you have to follow carefully every time you want to add another component to your entity, or build a new entity from scratch. You really don't want to end up in that situation when it can be prevented :)


Well I think I'm mostly ok then... I don't THINK I have components referencing other components directly... as far as I remember.

It's mostly just that I'm accessing components' functions from the program's main function for convenience's sake rather than wasting time on implementing a neat and more well-designed interface.

Quote:Original post by Trefall
That said, if you ever find the time to implement components into a proper application, I'd highly recommend to search through the web and review as many approaches as you can, then mix them together into the component system that fits yourself and your brain the best.


Yeah, I've been researching components for a while now on and off since it seemed like a very well-organized and logical approach especially if you're using XML or something and want to avoid hard-coding, I just never got around to actually trying to implement it.

I've at least learned a lot about how it works now. That might encourage me to experiment with it more in future for a game, as long as I haven't been put off by problems implementing communication and whatnot ^_^.
Advertisement
Ok, one final question:

When should I add components as observers of other components?

I could do it when a component is added to an Entity, so that it looks for whatever type of component it needs to observe, but then that means components need to be added in a specific order. That's not horrible I suppose but it sounds bad.

Apart from being added to an Entity, there's nothing else that all components do before they start being used. Should there maybe be a function like "addAsObserver()" in every component and then I call an Entity function like "initObservers()" that calls every component's "addAsObserver()" function?
That's about my best idea right now...
For this project I wouldn't really bother with trying to generalizing. Rather I'd do a 3 step construction of a new entity:

1) Instantiate entity
2) Add components
3) Bind observers

This has a couple of requirements. You have to know which component needs to be added as observer to which component (eg. Physical->addPosObserver(Collision) ), and components has to be added in the same scope in your code.

Just refer back to the Usage section in my first post on this. That should work, though there I followed:

1) Instantiate entity
2) Instantiate components
3) Bind observers
4) Add components to entity

Quote:Original post by Sean_Seanston
I've at least learned a lot about how it works now. That might encourage me to experiment with it more in future for a game...


If so, I guess I've accomplished something here ;)
Alright, cool. I'd actually forgotten about that part in the original code :p.

Should be fine from here on.

Quote:Original post by Trefall
If so, I guess I've accomplished something here ;)


Yep ^_^.

This topic is closed to new replies.

Advertisement