Sign in to follow this  

How to program an Inventory

This topic is 3317 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

I'm working on a text-based RPG in C++. The game is going to consist of a player exploring a world, battling monsters, acquiring gear, potions, treasures, and interacting with various NPCs in towns and other points of interest. My question is pretty straightforward: How should I go about programming the player's inventory? The player is an object of the Player class. Items and in general, things that would be in the inventory are objects of the Item class. Should I just have a resizable array called Inventory which is a member of class Player and of type Item? Using an array that will be resized at runtime seems inefficient to me. If this were SQL, I'd have a player table, an item table, and an inventory table which contained all player_id, item_id pairs.

Share this post


Link to post
Share on other sites
I agree. std::array should suit you perfectly, although I myself would use an std::list for the easy insertion and deletion brownie points. Unless you have 40,000 Items in the list and you are sorting and/or traverse that array 60 times a second, then you have no worry as to being "inefficient" (that is, "inefficient" being "slow/complicated").

As a suggestion to your inventory, you might want to consider taking advantage of std::sort and provide an operator < in the Item class to define the rule on which Items are sorted.

Share this post


Link to post
Share on other sites

This topic is 3317 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this