C++ Inventory

Hi Guys

I'm designing the inventory for another small rpg Project. I was wondering what would be most efficient to create lots of items.

I could of course create a base class and then create lots of Abstract classes. In my opinion this will get very bothersome to expand later on. It's not a big deal as Long as I only have one or two diffrent items, but its semiperfect.

Any opinions how I could go around it? Should I write a list in a txt-file with sequentially numbered items and their effects and then read their data as soon as the Player gets it? Wouldn't this kinda System be very vulnerable to cheating?

Thankx and Greetz
Last edited on
create a base class and then create lots of Abstract classes

Lots of abstract classes won't help you. Abstract classes are meant as base classes or interfaces.

One base class item and some derived classes could be a good idea, .
Can you tell us sth. about the items and how they differ?

Should I write a list in a txt-file with sequentially numbered items and their effects and then read their data as soon as the Player gets it? Wouldn't this kinda System be very vulnerable to cheating?

To prevent cheating you could encrypt the file.
Id make a base class with mainly get and set methods, constructor would be a dynamically allocated array, of course don't forget a deconstructor. And yes you can do like what Thomas said and encrypt the file.
Last edited on
Well, the Player itself consists of the following stats right now:
- HP
- Atk
- Def
- Level

Thats pretty basic and I'll expand it later on with things like Mana, Magic Power etc...

The items should be boosting those stats. The things I'll on the items list will probably look something like this:
Item Number, Index Number (e.g. potions, weapons, etc.), ItemName, HP-Boost, Atk-Boost, Def-Boost, UpgradeTo, SellPrice, BuyCost, LevelRequirement, Atk-Requirement, Def-Requirment.

Its an option that I'll create seperate lists for the diffrent Categeries (Index Numbers) so that I'll be able to set things like duration for potions only. But thats not gonna happen before I've found a good and stable way for the inventory. At first its gonna be very inefficient because many boosts of item will end up as 0.

My idea was to create a class called item and looking somehow like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Item
{
    private:
        int ListNumber;
        int IndexNumber; //category
        std::string Name;
        int HP-Boost;
        int Atk-Boost;
        int Def-Boost;
        std::string UpgradeTo; //could be integer pointing to ListNumber of the upgrade
        int SellPrice;
        int BuyCost;
        int LevelRequirement;

    public:
        Item();
        ~Item();
        //Setters and getters to all variables above
        
}


I meant derived classes by abstract ^^. I'm sorry, I didnt learn programming in english (shame over me, YEAH IT WAS A MISTAKE) and I kinda end up messing up the words.

Encryption is a nice suggestion :D (Typed from my mobile, be merciful with grammar and syntax)

Thanks for the help :)
Last edited on
I'm designing the inventory for another small rpg Project. I was wondering what would be most efficient to create lots of items.


You might want to check out: http://gameprogrammingpatterns.com/prototype.html

You might want to read through that entire site, in fact. ;)
Might want to change:

1
2
int sellPrice;
int buyCost;


to:

1
2
double sellPrice;
double buyPrice;


You can keep the names how you had them, but for me I like to organize my fields of data so it's easier for me to remeber/reference. I don't know what kind of RPG you are doing, but I usually do my prices with doubles, unless your doing a gold currency that doesn't have any decimals than you can keep 'em at ints!
Last edited on
Actually I'd keep it as an integer and use it as for example a number of pennies. This way you don't ever have to worry about precision or epsilon values and it's generally easier.
Meh yeah it really all comes down to the structure of the program
@cire: thanks for the link, this is defnitely very useful not only for this Topic, but for future coding as well. I've read some samples and Feedbacks on the book and decided to order it to Support Robert :D

@MattBess: Right now I'm going to Keep them at ints, because it affords less concentrating imo. I'll Keep it in mind though if I'd run into any Kind of related Problems there.

Summary: So one way would be a class called Item that loads all the important data from an encrypted file. Another way would be the prototyping suggested by cire and his link (you should definitely check it out!!). Do you guys have any other suggestions? :)

Thanks, you really helped me out so far :D
If you wanna get fancy you can hook it up to an online database :P
Topic archived. No new replies allowed.