Game Development Community

Designing an effecient inventory structure?

by Rodney (OldRod) Burns · in Technical Issues · 09/25/2003 (7:19 am) · 3 replies

I'm attempting to design a useful, but efficient data structure to handle items in an RPG.

Here's an example of what I'm talking about:

typedef struct sItem
(
    char name[32];
    char descript[256];
    float weight;
    long category; //enum containing flags like money, weapon, container, etc.
    long flags; //enum containing flags like droppable, sellable, etc.
    char iconfile[16]; // icon to display in inventory
    char meshfile[16]; // mesh file to load when displaying object
}

There's more, but you get the idea.

What I'm struggling with is how to handle items that grant attribute bonuses. Let's say the player finds a ring that grants him +5 HP and +5 AC. Now, I can make separate values for each attribute that the item can enhance, like this:

long hitpoints; // number of hp granted
     long ac; // ac increase
     ...

but if there are 10 attributes (or more) that seems wasteful, especially since a given item may only affect 1 attribute, or even none.

Or lets say the player finds something that is flagged as "ARMOR", how do I define what slot it goes in, without wasting space for all items that are not armor?

Does anyone have a suggestion of a good way to handle something like this in a data structure?

Thanks :)

#1
09/25/2003 (7:56 am)
For bonus items i use strings in a format like STR +5 INT -2 etc. where word 0 is bonus type and word 1 is bonus amount (separated by spaces), then word 2 is bonus type again, word 3 bonus amount etc.
This is not fast, but it's very handy (expecially in TGE scripting, where you have getWord and setWord functions). Besides, you don't really need it to be fast in most circumstances.
I would add an "inventorySlot" variable to the structure, so that you know where the item is inside the inventory and in a multiplayer game the server knows which item the client is selecting when he clicks an inventory icon. Then you could define the max number of inventory items, and decide that if inventorySlot > maxInvItems, then the player is actually wearing the item in slot (inventorySlot - maxInvItems).
At least, this is what i do and it works fine :)
A small hint: if you plan on developing containers for your game, think ahead and make your inventory a member of the container class, it will save you a lot of work later on.
#2
09/25/2003 (11:30 am)
Use inheritance or better yet template policies and have a virtual method called "applyModifications" that takes a player object then each one can manipulate the players stats independant of one another, it is not that difficult. Parsing strings as Hadoken mentions is really hacky and error prone and not maintainable over a large scale project.

There are LOTS of examples of things like this in RPG Game design books. If you are doing it in C++ use an OO approach the cleaner and more obvious the interface to the objects the more maintainable it will be.
#3
09/25/2003 (12:20 pm)
Naw use bitmasks..
one mask for each attribute
and each mask comprised of bits relating to value