• Advertisement
Sign in to follow this  

Inventory system framework... (I need help)

This topic is 3456 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 am still pretty new to game development. I have a couple books I have been working out of and have about two years of javascript experience (the engine I use uses javascript as the scripting language). The trouble I am having is trying to wrap my head around this inventory system and whole 'game object' concept. I know javascript doesn't have a true inheritance system, but I know about prototypes and parasitical inheritance which does the job, but then this rises a new question, why even have a base game object class? Anyway, I was planning on havnig a system similar to a slotted bag inventory, such as you would find in Daoc, WoW, etc. There are n number of bags and each bag can hold n number of items. For argument sake, lets say the inventory is 5 bags, each bag can hold 8 items. The items are not the tetris/diablo2 style, but one slot = one item. So I thought I would create an Inventory system like this:
function Inventory()
{
    bag = new Array([5]);
    for(var i1 in bag)
    bag[i1] = new Bag([8]);
    
    this.getBag = function(bagNumber){return bag[bagNumber];}
}

function Bag(size)
{
    var slot=new Array(size)
    for(var i1 in slot)
    slot[i1] = new Slot();

    this.getSlot = function(slotNumber){return slot[slotNumber]}
}

function Slot()
{
    var item = itemList[[0]];

    this.getItem = function(){return item;}
    this.setItem = function(i){item = i;}
}

So now to create this I just call something like:
var playerInventory = new Inventory();
//to set an item in a slot
playerInventory.getBag([0]).getSlot([5]).setItem(masterItemList[[25]]);

//to get an item in a slot
playerInventory.getBag([0]).getSlot([5]).getItem();

Is this the best way to do this? Am I supposed to pass a game object into the slot, or just the item number such as '25' instead of masterItemList[25] ??? As far as the game Items are concerned, I was going to have different constructors for different item types as opposed to the base game object-> inheritance structure...
function WeaponItem(weaponName, weaponDescription, weaponIcon, weaponMesh, weaponScript, etc)
{
    
}

Is this ok? Can anyone throw me a bone as far as a good tutorial or e-book or something to read? "Developing Games in Java by David Brackeen" doesn't even touch on an Inventory system, and "Programming Role Playing games with DirectX 2nd Edition by Jim Adams" covers them, but he uses one single list of items and it isn't very in depth in my opinion. The main thing is I want to know when to create a NEW item as opposed to passing in an item integer, and what other people have done for something similar.

Share this post


Link to post
Share on other sites
Advertisement
(Disclaimer: I don't know javascript)

Bag, Slot and Inventory don't seem to have any more functionality than an array, so unless you add more functionality, you could get by with just nested arrays.

If items can change their state (e.g. acid attacks corroding your armor), you need a new item instance for each slot. If they can't change state, you can use an item number or a shared item instance.

Share this post


Link to post
Share on other sites
The system I use has a single, large slotted bag. Each slot, programmatically, simply holds an integer value assosiated with an item, and the number of items in said slot. The masteritemlist is only looked at when what an item is/does is needed.

When adding an item, it checks the bag for an existing item of the same type (integer id), and if it finds it, cross references the number in inventory to the max number that stack in one slot in the master item list info list (basically a vector), and either adds to the stack, or adds a new stack.

When an item is used, i register the click, figure out what slot was clicked, check what item it is, and use that items use function, which is passed an entity reference (in almost all cases, the player itself), and the item in the list applies the use effect.

Basically, if a healing potion is in slot 1, and the player clicks on it, the system figures out that slot 1 is a potion, and the potion item in the master list is passed the players entity, and the potion adds 35-55 to the passed entitys health.

Same goes with equiptment, i dont have random stats on stuff, so each inventory slot is just a reference to a piece of gear, which modifies the players stats and whatnot. The stats that are changed are updated only when a piece of gear is changed, so everything stays nice and cached in the mean time.

Im not sure if its the best way, but im assuming the topic of "best" way could spawn a large debate. My system works perfectly for me, so i consider it the best for me, at least until i need it to do something else.

Share this post


Link to post
Share on other sites
Hey thanks for those replies guys. I sort of thought I was making it more difficult than it had to be.

So from what I understand, putting together both of your replies:

1. I can just use a double array for slot[][].

2. Only create an instance of the item if the items base state can be changed via game play.
3. 'Best' is a relative term.

Thanks guys!

If anyone else has something to add, please please do.

Share this post


Link to post
Share on other sites
I only know javascript for a bit of DOM stuff in HTML when I needed a bit of extra functionality.

A double array would work fine but if I was to add one to my game I would make an Inventory object similar to what you have and have all the functionality in there, as to hide it (encapsulate, I'm a bit of an OOP fan) from the rest of the program.

I'll just write up some psuedo-code how mine would look
class Inventory
{
private const int MAX_BAGS = 5;
private ListOfBags bags;

// Item adding/movement/destroying functions all here
}

class Bag
{
private ListOfItems items;
private int maxSlots;
}

class Item
{
// Describes everything about an object, will differ depending on what you want.
}
An upside to a system like this is how it hides everything in separate classes so you can implement them and basically never look at them again :)

A master list would probably be the easiest to add, but the screwing around with items inside your character or game code pieces would be messy and easy to cause errors.

As for when to create items or passing integers, it all depends on how flexible your item system is. The game I'm making at the moment is similar to that of Diablos; <prefix> <item> of the <suffix>. Each prefix and suffix add to 1 or more stats. So each item in my game needs to be created (randomly) and stored in an item object, since nearly every item is unique.

I have an enumeration (a list of words that represent numbers; 0,1,2,n-1) called StatType which houses every single stat in the game, this allows me to easily attach a StatType and a number to add/decrease individual stats on items, skills etc.

This item/stat system is probably a more advanced then you are looking for but just sharing how I went about making mine.

Share this post


Link to post
Share on other sites
That is exactly what I was looking for. I started to think about the double array idea, and because the bags will be able to be swapped out, such as a 4 slotted bag vs an 18 slotted bag, I realized I would still have to create a bag object which holds the icon and values of the bag.

That is a neat idea of the prefix_item_suffix naming system. I know a few games do that, and it is nice because if your weapon is 'of Might' or 'of Fortitude' you know exactly which stat(s) is would raise.

So in your case, you would set

inventory.getBag(n).getSlot(n) = new itemObject(prefixList[n], itemList[n], suffixList[n]);

The master item list is just what I have seen a couple games do. I guess you could still pass in three integers representing the prefix, item, and suffix and then just reference the enumeration values when doing checks. I wonder which one is better as far as performance goes.

Anyway, thanks for the input and insite. If anyone else has something to add, please do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement