How to make a world-to-player inventory system?

I am running into an issue of trying to implement an inventory system within XNA (doesn't really matter, could be any platform) but so far. Here is the issue I am trying to wrap my head around:

How can I get an item from the world (think when you kill a monster it drops armor) and get it into my player's inventory?

If an inventory is just a list of items, technically, how could I get Steel Sword from the world into my player's inventory without having a class do more than one thing?

I currently have an item struct, which contains a unique hex value, a count of how many items there are (for stackables, like arrows) and a Texture for rendering within the inventory, and some other unimportant properties (name, description, etc).

It would be bad design if I had to modify my existing item class to house a world position, along with collision detection (don't want the item falling through the earth).

Thanks for any considerations!

• Can't you just have a WorldItem which is seperate to your InventoryItem? (Maybe the WorldItem has an InventoryItem template that's put into the player's inventory when picked up). – George Duckett Jan 4 '12 at 13:28
• @GeorgeDuckett You should make this an answer.. there should probably be some method in the Inventory that takes a WorldItem and adds it as InventoryItem. So that it could also handle cases where items need to stack (eg. Arrows). – bummzack Jan 4 '12 at 13:36
• @bummzack: Done. – George Duckett Jan 4 '12 at 13:46

The approach I would go for is to have 2 different structs (or classes). WorldItem and InventoryItem.

You could then add an InventoryItem as a property/field of your WorldItem which is what gets placed in the player's inventory when the world item is picked up.

You'd want a method somewhere (on the player class, or either item class) that handles adding an inventory item to the player's inventory, to deal with stacking like items.

• I really like this answer, thank you so much, it makes so much sense! One last thought, let's say the person wants to drop an item, would the Inventory item also need a WorldItem within it as well? – Ross Jan 4 '12 at 14:23
• If it needs to be 2 way I'd be tempted to have a separate 'lookup' class that stored pairs of world items and inventory items. You'd then use that to look up one, given the other. – George Duckett Jan 4 '12 at 14:28
• Ohhh nice, never thought of that. That is a very interesting idea. Since all items are concrete, it makes sense to have a lookup for them. – Ross Jan 4 '12 at 14:43
• I'd destroy the WorldItem when it is picked up and create a new one when the item is dropped. – Simon Richter Jan 4 '12 at 17:20
• Agreed. Didn't mean to imply otherwise. – George Duckett Jan 4 '12 at 19:04

As mentioned before splitting up into multiple classes or arrays would be your best bet. I'd create 3 classes, World, Player and Item The world class would have a List WorldItems in it, so would the Player class, but named List Inventory When a player picks up an item, you move it from the list in the World class to the list in the Player class.

In the draw loop of your game you could make a loop which draws all the Items in the WorldItems list to the screen, but off course ignores the Inventory list on the player object.

• My current method right now is that I have a WorldItem class that encapsulates an InventoryItem, both pertaining to the same item. In the Game's Update method I cycle through all world items and test collision boundaries with the player. If the two collide, the WorldItem is removed from the WorldItem list and is put into the player's inventory (which is a List<> of InventoryItem) – Ross Jan 6 '12 at 14:48