I'm trying to make an inventory system for a rpg game, and I'm having a hard time doing it. This is the idea of what I have so far:
So the idea is that each value in my inventory array points to an item name and quantity.Code:class Item { public: void create_item(int amount){quantity += amount;} string name; int quantity; } potion = {"Potion", 0}, antidote = {"Antidote", 0}; class Inventory { public: string* name; int* quantity; }inventory[100]; void fill_inventory() { inventory[0].name = &potion.name; inventory[0].quantity = &potion.quantity; inventory[1].name = &antidote.name; inventory[1].quantity = &antidote.quantity; } void display_inventory() { for (int i = 0; i< 100; i++) if (*inventory[i].quantity > 0) cout << "Item# " << i+1 << " " << *inventory[i].name << " " << *inventory[i].quantity << endl; }
Then display_inventory() will show the user all of the items they "have" ( more than 0). To add an "item", all I would have to do is call that objects add function, and the Inventory pointers would "update" to show the correct quantity;
Is there a better way I can have the Inventory members point to their corresponding Item members? I wish I could just do inventory[0] = &potion; and the compiler would know to make inventory[0].name = &potion.name, and inventory[0].quantity = &potion.quantity
And my second question is: I'm going about this all wrong aren't I? Haha.



LinkBack URL
About LinkBacks



