I'm working on my first game, and taking the opportunity to brush up my C++ (I want to make as much of it as portable as I can.)

Whilst working on the technology tree and how it affects actors (spaceships, planets, crew, buildings, etc) I can't find a pattern that decouples these entities enough to feel like a clean approach.

Just as an idea, here's the type of effects these actors can have on one another (and techs too)

  • An engineer inside a spaceship boosts its shield
  • A hero in a spaceship in a fleet increases morale
  • A technology improves spaceships' travel distance
  • A building in a planet improves its production

The best I can come up with is the Observer pattern, and basically manage it more or less manually (when a crew member enters a spaceship, fire the event; when a new building is built in a planet, fire the event, etc etc.) but it seems to be too tightly coupled to me.

I would love to get some ideas about how to approach this better.


1 Answer 1


I would say that you could probably approach this the same way that RPGs and MMOs handle 'buffs' and 'debuffs', as these are modifiers applied to a base value that can be added or removed based on some condition.

As such, the answers here may help: What's a way to implement a flexible buff/debuff system?

I would warn against an event-driven system for this situation as they are prone to 'drift' when trying to apply and remove complex modifiers. (eg. If you apply a x2 modifier, then a +10 modifier, remove the x2 modifier, then remove the +10 modifier, it's easy to accidentally end up 5 points below where you started.) For this reason, I don't think an Observer pattern is ideal.

Instead, I would poll the values when needed. When checking shields, count the engineers inside. When checking morale, look for heroes in the spaceships in the fleet. If this turns out to perform poorly, you can cache the values or precalculate these queries.

  • \$\begingroup\$ And all this time I was doing the wrong search! Thank you for the great answer! \$\endgroup\$
    – changelog
    Commented Jul 4, 2012 at 8:25

Not the answer you're looking for? Browse other questions tagged .