views:

153

answers:

2

In my spare time I program games as a hobby, different sorts of things and currently nothing very complex. Things likes 2d shooters, Tile-Based games, Puzzle Games, etc...

However as the development of these games goes on I find it becomes hard to manage the complexity of different subsystems within the games, things like Interface, World View/Model, Event Handling, States (Menu's, Pause, etc...), Special Effects and so on.

I attempt to keep the connections to a minimum and reduce coupling however many of these systems need to talk in one way or another that doesn't require holding your entire code-base in your head at one time.

Currently I try to delegate different subsystems and subsystem functions to different objects that are aggregated together however I haven't found a communication strategy that is decoupled enough.

What sort of techniques can I use to help me juggle all of these different subsystems and handle the complexity of an ever increasing system that needs to be modular enough to facilitate rapid requirements change?

I often find myself asking the same questions:

  • How do objects communicate with each other?
  • Where should the code that handles specific subsystems go?
  • How much of my code base should I have to think about at one time?
  • How can I reduce coupling between game entities?
+7  A: 

Ah, if only there were a good answer to your question. Then game development wouldn't be nearly as difficult, risky, and time-consuming.

I attempt to keep the connections to a minimum and reduce coupling however many of these systems need to talk in one way or another that doesn't require holding your entire code-base in your head at one time.

They do, but often they don't need to talk in quite as direct a way as people first believe. For example, it's common to have the game state push values into its GUI whenever something changes. If instead you can just store values and let the GUI query them (perhaps via an observer pattern), you have then removed all GUI references from the game state. Often it's enough to simply ask whether a subsystem can pull the information it needs from a simple interface instead of having to push the data in.

  • How do objects communicate with each other?
  • Where should the code that handles specific subsystems go?
  • How much of my code base should I have to think about at one time?
  • How can I reduce coupling between game entities?

None of this is really specific to games, but it's a problem that arises often with games because there are so many disparate subsystems that we've not yet developed standard approaches to. If you take web development then there are really just a small number of established paradigms: the "one template/code file per URI" of something like PHP, or maybe the "model/view-template/controller" approach of RoR, Django, plus a couple of others. But for games, everybody is rolling their own.

But one thing is clear: you can't solve the problem by asking 'How do objects communicate'. There are many different types of object and they require different approaches. Don't try and find one global solution to fit every part of your game - input, networking, audio, physics, artificial intelligence, rendering, serialisation - it's not going to happen. If you try to write any application by trying to come up with a perfect IObject interface that will suit every purpose then you'll fail. Solve individual problems first and then look for the commonality, refactoring as you go. Your code must first be usable before it can be even considered to be reusable.

Game subsystems live at whatever level they need to, no higher. Typically I have a top level App, which owns the Graphics, Sound, Input, and Game objects (among others). The Game object owns the Map or World, the Players, the non-players, the things that define those objects, etc.

Distinct game states can be a bit tricky but they're actually not as important as people assume they are. Pause can be coded as a boolean which, when set, simply disables AI/physics updates. Menus can be coded as simple GUI overlays. So your 'menu state' merely becomes a case of pausing the game and showing the menu, and unpausing the game when the menu is closed - no explicit state management required.

Reducing coupling between game entities is pretty easy, again as long as you don't have an amorphous idea of what a game entity is that leads to everything needing to potentially talk to everything. Game characters typically live within a Map or a World, which is essentially a spatial database (among other things) and can ask the World to tell it about nearby characters and objects, without ever needing to hold direct references to them.

Overall though you just have to use good software development rules for your code. The main thing is to keep interfaces small, simple, and focused on one and only one aspect. Loose coupling and the ability to focus on smaller areas of the code flows naturally from that.

Kylotan
Excellent answer, just what I was looking for. It seems my current design has been suffering from trying to make the perfect "IObject". Thanks again.
VoDurden
+1 for *"Solve individual problems first ... refactoring as you go."*
BlueRaja - Danny Pflughoeft
+3  A: 

I'm working on a book on exactly this question: Game Programming Patterns.

munificent
Looks interesting, I'll have to keep an eye on this.
VoDurden