Managing items in a tile-based world?

Started by
3 comments, last by GameDev.net 24 years, 4 months ago
There's two ways you can get around that problem. One is to use templates, which I personally hate and haven't entirely figured out yet, but they make the code really ugly

The other way is to use inheritance. Say you have a base class for your weapons, CWeapon, that every other weapon derives from. Then you can create instances of the new classes using a CWeapon pointer, e.g. CWeapon *gun = new CRailGun. Then you could create a list that stores CWeapon pointers and insert the weapons into that list. You'd have to make certain functions of CWeapon virtual to make sure that the derived classes have things called right, but it should all work out.

Jonathan

Advertisement
Suppose you have a class Item and two classes Minigun and HealthKit, which are both derived from Item. e.g. in C++

class Item
{
...
};

class Minigun: public Item
{
...
};

class HealthKit: public Item
{
...
};

Using the STL, which comes with VC++, you can get a linked list of any kind of Item like this.

std::list itemList;

You can put items in the list like this.

HealthKit healthKit;
...
itemList.push_back(healthKit);

By the way, if you're having trouble with this, I suggest you read a good book on OOP before you try and write a game!

if you go the inheritance route....

(and listen closely here)

virtual virtual virtual

and i repeat

virtual virtual virtual

(got that?)

any member function that you even THINK you might change in an inherited class, make virtual. make the destructor virtual.

you arent just inheriting in this case. in this case, you are polymorphing.

Get off my lawn!

I'm relatively new to OOP in general, and I've got a few questions on the best way to handle items in a tile-based environment.

If I were to create classes to mimic the various items I were to have (weapons, health kits, etc.), how do I go about storing these items? If I wanted a linked list, doesn't it have to be ONE particular class? If so, how do I store all my items in one list when they are of different types? Perhaps I'm going about this the wrong way, if so please set me straight

The great thing about OOP is inhertience and, polymophisum.

Best way of doing this is to produce an abstract base class ie a class witch only contains pure virtual methods and no data stores.

Also remember you can inherit data structures inside your class as well as other classes.

The = 0 decears the method does not exist by setting it's pointer to NULL but that is probably going a bit to low for now.

Abstract class:

class cABC{
cABC();
virtual ~cABC() =0;

virtual Method1() =0;
virtual Method2() =0;
.
.
.
};

Data structure inheritence

struct sLIST{
sLIST *Prev;
void *Data;
sLIST *Next;
};

class cLIST : public sLIST
{
cLIST();
~cLIST();

GetNext();
GetPrev();
GetFirst();
.
.
.
MethodX();
};

This topic is closed to new replies.

Advertisement