Game Design Music and Art

Object-oriented overkill? – Briant

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
I used to be a old-style C programmer. But since learning C++ several years ago, and programming with it most of the time since, I find myself automatically thinking in terms of "objects" when tackling a programming problem. I simply write down in words the problem I am attempting to solve, and voila, all the main nouns become classes and all the verbs become member functions.

But at what point does this approach become overkill? I mean, how do you decide between:


enum Guntype { pistol, shotgun, sniperrifle, laserrifle, plasma };

struct Gun
{
Guntype type;
int maxcapacity; // max ammo
int currcapacity; // current ammo
};

void fire(Gun *gun)
{
if(gun->currcapacity == 0)
{
ClickSound(gun);
return;
}
gun->currcapacity--;
DoFancyGraphicsAndSounds(gun);
...
}

and


class Item
{
...
};

class Weapon : public Item
{
...
};

class Gun : public Weapon
{
protected:
int maxcapacity; // max ammo
int currcapacity; // current ammo
public:
Gun(int init_maxcapacity);
virtual ~Gun();
int Fire();
...
};

int Gun::Fire()
{
if(currcapacity == 0)
{
ClickSound();
return;
}
currcapacity--;
DoFancyGraphicsAndSounds();
...
}

class Rifle : public Gun
{
...
};

class LaserRifle : public Rifle
{
...
};

class SniperRifle : public Rifle
{
...
};

class Pistol : public Gun
{
...
};

Is this purely a personal design issue, or is there practical reasons to choose one over the other? I read tutorials and books, and they are still doing the "old C-style" method for everything (verticies, polygons, weapons, players, etc.) while it would be really clean and easy (I think) to use objects instead. I've even been skimming some of the Quake3 code, and its ALL old-style. Why?

Brian

Krylar

Administrator

Posts: 502
From: MD, USA
Registered: 03-05-2001
Hiya,

I'm an old-school ANSI C coder and have found it pretty tough to get into the OOP mindset. But I can say that I've found classes to be a BIG help in a number of ways. It makes code much cleaner and it allows me to focus on reusability. Does this mean I can't code straight C to be reusable and clean? No, but the point is that I DON'T typically do that. C++ kinda forces the issue...and I think that's a good thing.

I have heard that using things such as Virtual Functions and Inheritance can cause big slowdowns in code and that's a big reason why game developers don't use C++ to its fullest...but I'm such a neophyte in the realm of C++ that I can't support or deny that argument.

So...I know a lot of developers that use C++ and a lot of others that go the straight C route. Both will argue that they're way is the best. I would argue that whatever method you use that makes you actually sit down and CODE YOUR APPLICATION is the best one to use That's why I now use BlitzBasic

-Krylar

------------------