[AGDev-newbies] OOP was engines and code.
Thomas Ward
tward1978 at earthlink.net
Fri May 5 00:25:09 BST 2006
Hi Richard and all.
Object oriented programming, (OOP,) is very simple to understand when
you think of it outside of a programming book. Programming books do a
poor job of explaining it so I will attempt to capture the idea for you
here.
Object Oriented Programming is all about things called objects.
Everything in our world can be thought as an object. The TV, the dog
sleeping on the couch, and even the couch itself is an object. However,
you need to code in a way to describe and interact with that object, and
break those objects down in to special instances of it. by calling it by
it's name. The dog is no longer just a dog, but the dog is named
Princess. So Princess is an instance of dog. An instance is a specific
object.
So in games you are going to want to create objects to populate your
game world such as monsters, walls, doors, weapons, lava pits, etc
right? Of course you want to and here is how it is done.
You start with a class. A class is what holds all the data for that kind
of object. Even nicer classes can be inherited so if there is an object
that shares something with another object it can inherit properties of
that other object's base class.
Let's start looking at how this is done in C++. Make sure say all
punctuation is on in your screen reader.
// Creature class.
// Base class for creating creatures.
class Creature
{
// Private area for class
// variables.
private:
int iHealth;
float fX;
float fY;
float fZ;
// Public class
// method prototypes.
public:
void AddHealth(int);
int GetHealth();
void SetHealth(int);
void SubHealth(int);
void SetLocation(float, float, float);
float GetX();
float GetY();
float GetZ();
};
For purposes of this example I will skip the portion of code that
actually does the adding and subtracting of health, as well as setting
and getting location. Just assume this class was fully working. We can
do some cool stuff with it.
We can add on to this class to make all kinds of creatures that share
health, as well as location. Of course, if this was a real game we would
be adding all kinds of stuff like number of lives, scores, whatever.
Let's now expand the Creature class to be say a dragon just for fun.
// Class dragon.
// Class of creature
// with fire breathing abilities.
class Dragon: Creature
{
// Dragon private variables.
private:
int iFire;
// Dragon public
// method prototypes.
public:
void AddFire(int);
int GetFire();
void SetFire(int);
void SubFire(int);
};
Now, we have expanded the creature class to be a dragon. If we wanted we
could create Orks, Humans, and other types of creatures and share the
Creature class between them. Since this is not a real game I am not
going to define them all.
However, where oop really shines is if you were to place the Creature
class in Creature.h and Dragon class in Dragon.h you could in theory
could copy the files, paste them in to new projects, and use them
forever over and over again.
Now, that you have the classes you need to create an object of a dragon,
and give it a name. Just for fun I'll name him Puff.
// Include class headers.
#include "Dragon.h"
// Global dragon objects.
Dragon g_Puff;
// Create a new instance of all game objects.
void CreateObjects()
{
g_puff = new Dragon();
}
At this point we have a object, something we can work with, a dragon and
it's instance is named g_puff.
Let's do something with this new dragon.
// Create Puff
// the magic dragon.
void CreatePuff()
{
g_puff.SetHealth(100);
g_puff.SetFire(100);
g_puff.SetLocation(61, 55, 33);
}
There you go. If you were following along you created a base class of
creature, expanded it to a dragon, created an object, named that
instance puff, and added health, fire, and set it's location.
Why is this good? Well, if you had to create many dragons and not just
puff you would normally have to have a set of variables for every
creature such as
fPuffX, fPuffY, fPuffZ, fPuffHealth, etc....
With object oriented programming you only write the variables once and
place them in classes. So if you do
Dragon g_GreenDragon;
Dragon g_BlackDragon;
Dragon g_RedDragon;
you are actually creating new instances of iHealth, fX, fY, fZ without
having to retype them over and over again for each dragon and other
creatures. Slick huh?
More information about the AGDev-newbies
mailing list