• Advertisement

• Popular Now

• 9
• 16
• 15
• 12
• 9
• Advertisement
• Advertisement
• Advertisement

c++ inventory program

This topic is 2810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

At the moment I am trying to implement an inventory program that will allow the player to buy and sell a variety items and weapons from the vendor. I also need to be able to add in new items as well. What I'm having problems is, is that I have never created a program like this before in C++.

To date, I have created 3 games using xna and C# and one, highly bugged c++ game in DirectX that already have 90% of the source code created (badly) for me. So this is my first, full on C++ game I'm working on.

Please could someone give me a pointer so I can at least make a start at coding this thing as I literally have no idea how to begin this.

Share this post

Share on other sites
Advertisement
A good place to start might be coming up with a representation for the inventory item, and then choosing a container to keep them in.

For the representation, if all you need is the name of the item, you could just use std::string. If you need more information (such as the cost), I would start with a simple struct or class with public member data (just for simplicity).

For the container, vector, list, and set would probably all be reasonable choices. (Those aren't the only choices available, but I imagine that at least one of them will meet your requirements.)

Share this post

Share on other sites
Thanks for the advice.

The information I need to show is: name, amount (both player and vendor), damage (if any) and cost.

The input for actually buying the stuff is being done by another team member.

Share this post

Share on other sites
Quote:
 Original post by DriveByBaptismThanks for the advice.The information I need to show is: name, amount (both player and vendor), damage (if any) and cost.The input for actually buying the stuff is being done by another team member.

well then
class InvItem{public:private:   char* m_name;   uint m_amount;   float damage;   uint m_price;   void* m_actualItem; //Pointer to the actual item, the type should actually be your items base class}

Could be your class to store the inventory items, I keep track of an item pointer so you want have to go into another system and search by name for this item to get the item itself.

Share this post

Share on other sites
This is what I've manage to do so far? I'm not 100% sure I've done the right thing. Does it look ok? I've never written a c++ class before.

class inventory{	private:		char* food;		char* drink;		int ammo;		int cost;		float damage; //for weapons		int player_amount;		int vendor_amount;	public:		void add_food(int amount);		int get_food();		void add_drink(int amount);		int get_drink();		void set_cost(int amount);		int get_cost();		void set_vendor_amount(int amount);		int get_vendor_amount();};

Share this post

Share on other sites
What exactly is the Inventory object? Is it an item that can be in an inventory, or is it the object that represents a vendor/character's entire inventory? The fields and methods make it seem like a mix of the two. The "cost" field is what is confusing me, really.

char* food and char* drink: what do these represent? If they're supposed to be words, I'd use std::string instead of char*.

Also, since it wasn't mentioned, this is only the header file for the class, correct? (Since all the method definitions are missing)

[Edited by - BCullis on July 15, 2010 11:21:09 AM]

Share this post

Share on other sites
This is to show the players and vendors entire stock of items. It will also allow the items to increase and decress depending if the player buys or sells any items.

Umm this was going to be my .cpp file. I have no idea how to make .h files.

Share this post

Share on other sites
hmm... I'd suggest some c++ fundamentals before continuing on, especially if your not sure what an .h file should contain. The gist of it is that the header file contains the class, and the source file contains the implementation. You and the other team member should sit down and hash out amongst yourselves what an item is, what an inventory is, and how they relate, before beginning to code. If your new to modelling, look up CRC cards. Once you have a good understanding of the problem, the solution will present itself.

Share this post

Share on other sites
Quote:
 Original post by DriveByBaptismThis is to show the players and vendors entire stock of items. It will also allow the items to increase and decress depending if the player buys or sells any items.

What you probably want then is a std::list or std::vector of various item types, if not a vector of base "item" pointers. Your methods would then operate on the list, like void displayList() and void addItem(Item incomingItem). You would then create an Item class, or several, that would include the data and operations specific to individual items.

Quote:
 Umm this was going to be my .cpp file. I have no idea how to make .h files.

What you have here is a class declaration, with the signatures of methods declared but no actual methods. Or were you just going to define your methods right below? This file as it is would make a perfect .h file, and then in the Inventory.cpp file you would just #include "Inventory.h" and start defining your methods.

I might want to add, IMHO, that given how you've "never written a c++ class before" (even though you cite having created a c++ game, if only 10% of it), building an inventory system might be a little intricate for your first from-scratch foray into C++. You might want to get better acquainted with the standard template library (namely the containers) via some smaller task before tackling this one...it's not impossible, but I get the feeling you'll be struggling uphill most of the way with a pretty shaky foundation.

[Edited by - BCullis on July 15, 2010 1:21:11 PM]

Share this post

Share on other sites
+1 to learning the fundamentals first, C++ is not a forgiving language if you don't know exactly what you do.

Regarding the general structure of your inventory, I'm not quite sure why you have hardcoded food, drink, money, and ammo etc.
This certainly works, but it is not terribly flexible. I implement my inventory with a kind-of-deque-like container (does not matter really), which contains tuples of amount and item type. Knowing the type-id of an item, you can look up the stats of a certain item in a table. Look at it as "poor man's flyweight" :-)

Food is an item just like money or anything else. Except food has a "consumable" flag rather than for example "wearable", and it has a "fill stomach" index to it's "value" rather than a "increase armour" one.

Money is just an item with no special stats. If you go to a shop, the shopkeeper could ask for 50 "money-items", but he could as well ask for cowries or sticks, it doesn't really matter.

Share this post

Share on other sites

• Advertisement