Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a small game, I need to add an unlimited inventory to the game but I am not sure how to do it.

I have an inventory system with a specific amount of slots, here it is:

<?xml version="1.0"?>
<player>
 <username> XXX </username>
 <password> XXX </password>
 <slot1> X </slot1>
 <slot2> X </slot2>
 <slot3> X </slot3>
 <slot4> X </slot4>
 <slot5> X </slot5>
</player>

I am open to switching to SQL or another data storage system, but I need a solution that would let players have as many items in the inventory as they would like; with no limit.

share|improve this question
    
You can have arrays/list in xml. And you can have List in C#. –  Kikaimaru Sep 4 '13 at 13:16
2  
I'm really not following the problem here. Do you just not know how to put a list in C#'s XML serialization? –  Sean Middleditch Sep 4 '13 at 17:02
    
It should be pretty easy in SQL. You just need to make a Player table (with a player ID # as the primary key), Item table (with an item ID # as a the primary key), and an inventory table (which has foreign keys to the player ID# and item ID #) –  Benjamin Danger Johnson Sep 4 '13 at 17:20
add comment

2 Answers

up vote 5 down vote accepted

You'll want to use a List. Probably a List<InventoryItem> or similar.

The documentation for List<T> is here.

Here is a set of answers on how to serialize a list to XML (you didn't say how you were saving your XML - this link gives you some options).

An XML file can store the list of items "inline", so it might end up something like:

<Inventory>
    <Item>...</Item>
    <Item>...</Item>
    ...
</Inventory>

On the other hand, if you switched to a database (using SQL), the traditional way to store something like this would be to have player table (with a "key" value, like an ID number) and a inventory table for every item in the game - each having an "owner" value that matches the ID of the owning player.

You would then use SQL to query for all the items in the inventory table owned by a given player (ie: having the matching ID). Something like:

SELECT item_name,item_whatever FROM inventory WHERE owner_id = <whatever>

The correct solution probably depends on the scale of your game. But if you're unsure - go with the XML to begin with - it is easier to implement - you can alway change later if the need arises.

share|improve this answer
add comment

Theres two basic ways to do this:

  1. The Magic Bag
    Containers themselves have fixed sizes, but there's no limit to your ability to put "bags" inside other "bags". Thus the only practical limit on your inventory is your ability to get more "Bags"

    This has the advantage that containers have fixed capacity (which might be easier to implement), and that you can "throttle" players inventory capacities by controlling the price or availability of extra "bags".

  2. The Holdall
    This seems to be closer to what you are asking for; the player has a single "main inventory" of essentially infinite capacity.

    This has the advantage of minimising overhead and micromanagement for the player, though might complicate implementation depending on you game's structure.

in either case, you will need to be familiar with your programming languages' relevant List or Array types.


As for storage, your problem is simply that you feel the need to specify carried (but not equipped) items as being in specific slots; this is completely unnecessary. in XML, simply have multiple <item> tags. This is perfectly legal.

In SQL-based databases, the main solution that I've seen is to have a table of items, each of which has field denoting who "owns" them, or what "container" they are inside.
This is trivially queryable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.