views:

166

answers:

5

This question's been bugging me for a long time. I've always wondered how game developers were solving certain problems or situations that are quite common in certain genres.

For example, how would one implement the quests of a typical role-playing game (e.g. BG or TES)? Or how would you implement weapons with multiple stacking effects in a first-person shooter (e.g. the Shrink-gun or Freezer from DN3D)? How would you implement multiple choice options with a possibly intricate decision tree leading to several different outcomes (e.g. the mission trees in WC)?

Are there any examples or other resources for that? Blogs? Books? Sourcecode?

A: 

If you read the Gang of Four, the entire creational patterns sections is geared towards creating a maze.

Chacha102
Yeah, and virtually none of them would be used in practice!
Kylotan
At least, probably not for making a maze :)
kyoryu
+4  A: 

Visitor.

Observer.

Command.

Proxy (for network games).

Just about any of the creational patterns.

Think, "scenegraph."

Games aren't really that different from other types of apps, at least not to the extent that people think they are. I say this having been a professional game developer as well as a professional non-game developer :)

kyoryu
+4  A: 

Remember that these aren't the typical software architecture problems that the usual 'design pattern' sets out to solve - they are game design problems, ie. a set of requirements for the end software. As such the typical software patterns don't really apply. And for that reason, there aren't really software patterns for the type of thing you describe because in most cases there is no agreed set of specifications and requirements across multiple games. These features typically depend heavily on the game design and the program structure follows from there. There are certainly examples given in various books - eg. the Game Programming Gems series is highly recommended - but they are just starting points which you must customise for your specific requirements.

eg. Quests - Can they be repeated? Can a quest be 'failed' or do they remain incomplete? Do they contain multiple stages? Are there criteria that must be met before the quest can even be offered? Is there a journal that needs updating? Is there an automatic reward at the end? What are the conditions for completion, and how and when are they checked? Are certain events triggered by acceptance of the quest, or by its completion? Is there a notion of a player's progress within a quest? Are some quests mandatory? Are quests ordered in some way? All this will differ massively from game to game, based on the design.

Now, there are a few techniques and approaches that game programmers find themselves using quite a lot, eg. embedding of a scripting language, spatial databases using 2D hashing or partitioning, finite state machines, etc., but these are really just generic tools that happen to map quite well to the problems game developers face.

Kylotan
*Design Patterns* are meant to solve software problems, not game design problems. One challenge we face is that in games, "designer" means something substantially different from what it means in other software industries, so generally the literature appropriate to software designers should be consumed by the software people, not the content people.
dash-tom-bang
To use your quests example, one obvious place where a design pattern would come into play is quest updates - on attaching to a player, a quest would likely attach itself as an observer to various events on the player (like the "I killed something" event or the "I picked something up" event.). If you look at something like the datablocks in the Unreal Engine, those fall somewhere between the Factory, Builder, and Prototype patterns.
kyoryu
(and, FWIW, I've seen GoF patterns, by name, not including Singleton, in the source code of AAA titles)
kyoryu
@kyoryu: that's exactly the type of information I was looking for! Scenegraph was also a very good keyword. Thanks!@Kylotan: +1 for mentioning "spatial databases", that was also completely new for me. Thanks for the input. :)
Baelnorn
Yeah, of course you can (and do) use design patterns in games, but they're not "specifically useful", just "useful". (For what it's worth, I wouldn't consider using Observer for handling quest events.)
Kylotan
A: 

I would say that most of them can be heavily used, depending on the kind of game some might be more useful than ohers.

If I had to choose some of the most influential in a game's design:

  • Observer: most of the logic is based in events and reactions.
  • ChainOfResponsibility: changing behaviours at runtime (powerups, etc.).
  • State: for obvious reasons.
  • Composite: inventory management, or a scene graph, for example.
fortran
A better example of a composite is a scenegraph. A node that is composed of other nodes implementing the same interfaces is practically the textbook definition.
kyoryu
updating the answer to reflect that
fortran
A: 

My humble opinion is that all patterns can be useful in games but that generally speaking the most powerful usage of patterns are the ones that emerge from development without targeting a specific goal up front.

In other words, patterns are a great vernacular for what is. They are terrible to use as implementation strategy, however, as their use in this way often leads to complicated, bloated, and slow code.

dash-tom-bang