Hierarchical Based Systems

Programming complex game scenes often leads to very large and unmodular classes. During my years as a game programmer, one of the systems I’ve discovered to solve this problem is Hierarchical Based Systems. In fact, some languages are even centered around this ideology, namely flash as3!

Structure of a Hierarchical System

So what exactly is a hierarchical based system? Well simply put, a scene is divided into nodes where each node is a useful object that “exists” in the scene. One can say nodes are modular logic code. The basic rule of a node, in my opinion, is that it must have a parent and a set of children. The node first runs it’s own logic followed by all its children logics. Therefore just by calling the root node’s logic functions, all its children (and their children too) have their logics called automagically.

Here’s an example of a game component:

class Component
{
public:
  std::list<Component*> children;

  void addChild(Component* component)
  {
    children.push_back(component);
  }

  void removeChild(Component* component)
  {
    children.remove(component);
  }

  virtual void update()
  {
  }

  void _update()
  {
    update();
    for (std::list<Component*>::iterator i = children.begin(); i != children.end(); i ++)
      (*i)->_update();
  }
};

This class serves as a template, where inheriting classes just have to modify the update() function. The framework programmer then calls _update() to update the object and all its children down the hierarchy.

As if this wasn’t useful enough, nodes can further improve coding in games by providing the platform for scene management techniques, such as scene-graphs and octrees. But more on that another time. :P

0 Responses to “Hierarchical Based Systems”


  • No Comments

Leave a Reply