Objects in Games

Author: Nathan Clegg

From rec.games.programmer:

Brian K Martin wrote:
Lately i’ve been teaching myself c++, but i really don’t have any good books. all i want to know is: what’s the advantage of c++ over c?

First, let me tell you what you, as a C programmer, can benefit from with C++. I’ll get to generic object-oriented advantages in a minute.

C++-like comments. These are those one-line comments that begin with a double-slash (//) and do not have to be terminated. Very handy.

Overloaded functions. This is quite a handy thing. Basically, overloaded functions are functions with the same name with different arguments, and usually with different body code. Say, for example, you have an output() function that will execute a [printf(“%d”, arg)] if you pass it an integer, but [puts(arg)] if you pass it a string. It gets a little more complicated than this, but maybe you can see how this would be convenient. Please know that most of the advantages of C++ are out of convenience. They aren’t efficient, and could be done in pure C (most of them) if you really wanted to, but C++ makes it easier and prettier.

Overloaded operators. This is also quite handy. Overloaded operators act the way you want them to. For example, let’s say we have a struct called ‘date’, which holds (that’s right) a date. We want to increment the date to the next day, so we say ‘date++’. Now, first you know that we can’t do that because C’s increment operator would never work with a structure. Also, even if we could increment some number within the struct, did we check for leap year? How about the switching of the months and years? We could set up a function to do this, but ‘date++’ is much prettier and easier to read (for C programmers) than “function_to_inc_date(date)”. That’s what overloaded operators allow you to do.

I know there are more, such as references and other things that I won’t go into now. Let’s get onto the general advantages of object-oriented programming.

As you already mentioned, objects are basically glorified structures with functions. The definition of an object is an entity holding both CODE (functions) and DATA (variables). So where are the advantages?

The best introduction/explanation/shower-of-advantages I’ve ever seen is LambdaMOO, an object-oriented MUD (email me if you would like a couple of telnet addresses to check out). It makes the object theory very clear and easy to understand, and the advantages will knock you over the head.

Object-orientation is especially handy in game programming (this *is* rec.games.programmer, after all). For example, let’s pretend we’ve got a game that involves the slaying of monsters. Now, everytime I slash a beastie with my trusty broadsword, I want to graphically show that blow. Now, each monster should react differently. We could pass a struct called “monster_type” to a function react_to_blow(), and it would have to decide what to do with it. OR...we could just call a MONSTER[x]:blow(). It wouldn’t matter which MONSTER it was; they all have a blow() function on them, as well as all the data they need to interpret it.

Another advantage of objects is inheritence. Inheritence was a really hard concept for me to grasp on to when I first learned C++, but maybe I can explain it better than the book I learned it from (and a MOO would teach this one perfectly). Inheritence is very efficient. If blow() was not a function defined on MONSTER, then when I tried to call it, the compiler would look at MONSTER’s *parents*. I’ve just decided that inheritence is too big of an issue to discuss in this message :) However, suffice it to say that a function that would be used by several different classes, can be defined once and passed on to other (possibly different) objects.

I hope this helps a little bit. If you need a better explnation, clarification on any of these points, or help with programming, feel free to email me at anytime. Good luck! Please know that I was in the exact same situation; I gave up on learning OOP when I found it difficult because I couldn’t figure out what the advantages of it were! But it has them. Also know that OOP isn’t suited to all projects. Learn to use it when it will be effective.

Nathan Clegg, nclegg@io.com

Email me , or tweet @redblobgames, or comment: