Game Object Structure: Inheritance vs. Aggregation (original) (raw)

Home

What It's All About

Who Is This Guy?

The List

Complete Archive

Personal

RSS Feed

People


Game Object Structure: Inheritance vs. Aggregation

By Kyle Wilson
Wednesday, July 03, 2002

Every game engine I've encountered has had some notion of a game object, or a scene object, or an entity. A game object may be an animated creature, or an invisible box that sets off a trigger when entered, or an invisible hardpoint to which a missile attaches. In general, though, there will be a game object class which exposes an interface, or multiple interfaces, to the systems that handle collision, rendering, triggering, sound, etc.

(c) FreeFoto.comAt Interactive Magic, and later at HeadSpin/Cyan, I worked with inheritance-based game objects. There was a base game object class, from which were derived other object types. For example, dynamic objects derived from game objects, and animated objects derived from dynamic objects, and so forth. When I got to iROCK, I found a similar scheme, but with some differences that I'll discuss in a minute.

There are several problems with a game object design based on inheritance. Some problems can be worked around (with varying degrees of difficulty). Some problems are inherent in an inheritance-based design.

(c) FreeFoto.comSo if an inheritance-based game object design is riddled with problems, what is a better approach? In redesigning the Plasma engine used by HeadSpin/Cyan, the software engineering team opted to flatten the game object hierarchy down to a single game object class. This class aggregated some number of components which supported functionality previously supported by derived game objects.

In a post to Sweng-Gamedev back in early 2001[1] -- around the time Cyan started rearchitecting Plasma -- Scott Bilas of Gas Powered Games described changing the Dungeon Siege engine from a "static class hierarchy" to a "component based" design. In the same thread[2], Charles Bloom of Oddworld described the Munch's Oddysee class design as being similar to that of Dungeon Siege.

From these data points, I'm willing to interpolate a trend. If moving from class hierarchies to containers of components for game objects is a trend in game development, it mirrors a broader shift in the C++ development community at large. The traditional game object hierarchy sounds like what Herb Sutter characterizes as "mid-1990s-style inheritance-heavy design" [8]. (Sutter goes on to warn against overuse of inheritance.) In Design Patterns, the gang of four recommends only two principles for object oriented design: (1) program to an interface and not to an implementation and (2) favor object composition over class inheritance [3].

Component-based game objects are cleaner and more powerful than game objects based on inheritance. Components allow for better encapsulation of functionality. And components are inherently dynamic -- they give you the power to change at runtime state which could only be changed at compile time under an inheritance-based design. The use of inheritance in implementing game object functionality is attractive, but eventually limiting. A component-based design is to be preferred.

[1] Scott Bilas. Post to Sweng-Gamedev. Feb. 14, 2001.

[2] Charles Bloom. Post to Sweng-Gamedev. Feb. 15, 2001.

[3] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.

[4] John Lakos. Large Scale C++ Software Design. Addison-Wesley, 1996.

[5] Herb Marselas, "Profiling, Data Analysis, Scalability, and Magic Numbers, Part 2: Using Scalable Features and Conquering the Seven Deadly Performance Sins." Game Developer, Jul. 2000.

[6] Scott Meyers, Effective C++, Second Edition. Addison Wesley Longman, 1998.

[7] Stan Lippman, Inside the C++ Object Model. Addison-Wesley, 1996.

[8] Herb Sutter, Usenet post to comp.lang.c++.moderated. Aug. 22, 2001.

I'm Kyle Wilson. I've worked in the game industry since I got out of grad school in 1997. Any opinions expressed herein are in no way representative of those of my employers.