Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help on inventory

Started by harleking May 25, 2006 at 3:22 PM 7 replies 1.4k views
Original Post
harleking
harleking
I'm trying to program an inventory in c++. I'm not really doing a game right now, I just want to write some reusable inventory code in case I need it later (and I was curious as to how this stuff is done). My vision of the inventory would be like one in one of those old-style rpgs, which is basically a (scrollable) list of items in text. When selected the user can use or dispose the item (for example). I thought of creating a class Item, which would hold every item's information like name, type etc. I was also going to store these in a STL list, and maybe attach that list to an object player (or something like that). My biggest question is how to make it so that when the user chooses to use a certain item that each item perform a different task? I guess the class Item could have a function called use() or something, but the code inside this function would have to change depending on the item such as healing health points vs. increasing the player's speed. I thought maybe creating some child classes that each did something different (ie a class for changing the player's stats the other for changing hit points etc.), but I was sure if there was a standard more "elegant" way of creating inventories. I've already searched for code examples but haven't found any. Any help or links to code (I'm sure someone already has had to solve the problem) would be greatly appreciated.
vbuser1338
vbuser1338
Could you not make an Item base class and derive specific items from it? You could pass a pointer to the player and the item performs its task. Here's a little example:
class Player;     // Declared somewhere elseclass Item {     public:          virtual void Use(Player *player) = 0;     // Over-ride in derived class};class HealthPotion : public Item {     public:          virtual void Use(Player *player) {               player->Heal()          }};


I don't know if this is what you meant by child classes. This is one way to do it, but there are probably lots more.
Hope this helps,
vbuser
harleking
harleking
It does help, however, I guess that means that I will have to make a different class for each type of item in it... oh well. Thanks for the help!
Ezbez
Ezbez
Well, that's probably the way to go.

However, you might only need to make, for example, one class for a Sword, and then just have the sword have stats that vary from instance-to-instance. I think you'd be surprised at how few actual classes you would need once you get down to it.

Another (more complicated) way, would be to use a scripting language that calls a script upon using an item. This would be a good way to go for easily moddable content. This is similiar to what Neverwinter Nights does. You can assign items scripts for many different actions including getting equipped, dropped, destroyed, or used.

Edit: Or you could combine these two methods.
Ravuya
Ravuya
Quote:
Original post by harleking
It does help, however, I guess that means that I will have to make a different class for each type of item in it... oh well. Thanks for the help!


This is more or less what I do, so I inherit my items from Item and override the virtual method Use (although I also retain a pointer to the world insertion queue which lets me shoot bullets into the world, for example, or drop flaming bits of debris from using the item).

So I have a couple of classes, like Weapon and Health Kit, that inherit from Item, and contain special Load methods that allow me to set their "unique" properties (icon, ammo type, size) from a file. I'm sure someone else has a better idea, but this is it for me, and it seems to work.
harleking
harleking
Sounds good, I think I'm going to give it a try.

The scripting solutions also sounds interesting, what language could I use for this. Is there a source where I can read more about implementing scripting languages.
Deamonslayer
Deamonslayer
towards the OP, I have found that if you use

typedef struct OBJ
{
char name[]
int x,y;
int TYPE
//..etc
}OBJ;

this is so you dont have to make a new class for each item.
ex:

OBJ sword;
OBJ sheild;

you can prob use the x and y in the structure to find were to blit it to screen when blitting. Hope this helps!
Kewl Softwarez -Time is what you make of it.
vbuser1338
vbuser1338
One note about the last post. That would work but you would end up with something like this:
void Use(Player *player,Obj &obj) {     if(obj.type == SWORD) player->Equip("Sword");     else if(obj.type == HEALTH_POTION) player->Heal();     else if ....}

This would certainly work but you would end up using a lot of if ,else if,else statements which would not be needed with seperate classes inheriting from a base class. This is just something to think about when you are chosing what to do.

vbuser

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.