11
\$\begingroup\$

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!

\$\endgroup\$
3
  • 4
    \$\begingroup\$ 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). \$\endgroup\$ Commented Jan 4, 2012 at 13:28
  • 1
    \$\begingroup\$ @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). \$\endgroup\$
    – bummzack
    Commented Jan 4, 2012 at 13:36
  • \$\begingroup\$ @bummzack: Done. \$\endgroup\$ Commented Jan 4, 2012 at 13:46

2 Answers 2

16
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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? \$\endgroup\$
    – Ross
    Commented Jan 4, 2012 at 14:23
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 4, 2012 at 14:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Ross
    Commented Jan 4, 2012 at 14:43
  • 2
    \$\begingroup\$ I'd destroy the WorldItem when it is picked up and create a new one when the item is dropped. \$\endgroup\$ Commented Jan 4, 2012 at 17:20
  • \$\begingroup\$ Agreed. Didn't mean to imply otherwise. \$\endgroup\$ Commented Jan 4, 2012 at 19:04
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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) \$\endgroup\$
    – Ross
    Commented Jan 6, 2012 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .