Temptation to Misuse Class Inheritance

It’s good advice for all OO programmers that inheritance between classes should only be used when the subclass has an is-a relationship to the superclass. It can be tempting to inherit from a class just because it has properties or behaviours that you want and inheritance is a convenient way to get them easily. The following is a fun story from a game developer dealing with some problems caused by them ignoring this advice.

Camera obscura

This is an older one, but Force 21 was an early 3D RTS which used a follow cam to observe your current platoon. Toward the end of the project we had a strange bug where the camera would stop following the platoon — it would just stay where it was while your platoon moved on, and nothing would budge it. The apparent cause was random because we couldn’t find a decent repro case.

This went on until, finally, one of the testers noticed that it happened more often when an air strike occurred near your vehicles. Using that info I was able to track it down. Because the camera was using velocity and acceleration and could be collided with, I had derived it from our PhysicalObject class, which had those characteristics. It also had another characteristic: PhysicalObjects could take damage. The air strikes did enough damage in a large enough radius that they were quite literally “killing” the camera.

I did fix the bug by ensuring that cameras couldn’t take damage, but just to be sure, I boosted their armor and hit points to ridiculous levels. I believe I can safely say we had the toughest camera in any game.

– Jim Van Verth

Source:
http://www.gamasutra.com/view/news/249475/More_dirty_coding_tricks_from_game_developers.php

Leave a comment