Diablo 2 style inventory system
Displaying 1-4 of 4 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Technetium

Just wondering if anyone has managed to make an inventory system like the one used in Diablo 2, Baldur's Gate 2, or Might & Magic 6. I've been working on one for a side project of mine, but I am having loads of problems.

Basically, every item in the game has a unique reference number. There is a general variable 'numitems' that is increased every time a new item is generated. Every character has the variables 'character[].backpackref[]' and 'character[].numbackpackitems' (which increases whenever an item is placed in the backpack and decreases when an item is removed from it). For the backpackref, the index is the number of the backpack item (not the same thing as an item's general reference number, though). For example, a character might have 2 items in his backpack, which would be backpackref[0] and backpackref[1]. Each of those would return the actual reference number of the item.

To allow things to go both ways, every item has a variable 'item[].backpacknum'. If the fifth item generated in the game happened to be the 3rd item placed in a character's backpack, then 'item[4].backpacknum' would return 2.

The problem is when an item is picked up (by clicking on it). If item X, which is the Y item in the backpack, is picked up, item[X+n].backpacknum must all decrease by one (where X+n = that character's numbackpackitems-1). Also, character[].backpackref[Y] needs to change to character[].backpackref[Y+1]. There are a lot of others that change, too. But so many of these variables are nested that I can't keep track of what I'm doing. It is a big, big mess. And it isn't working, because what happens is that, if an item in the backpack has a higher item reference number than another item in the backpack, but has a lower backpack reference number than the other, and the one with the lower backpack number is picked up, it messes up the other one (the graphics get replaced with the item I picked up, or no graphics are displayed at all).

Anyways, I can't really explain the problem well enough for anyone to solve without pasting all the code, and I don't think anyone wants to read all that garbage. I'm really just wondering if anyone has created an inventory system like this, and if they have any suggestions for how to go about it.

Posted on 2004-11-18 20:55:11

Omni

Why do you need each general item structure to reference its own position in the player's backpack? That seems superfluous, and maybe cutting that would relieve a source of problems.

Other possibility: Don't shift items up or down in sequence when others are removed or added to the backpack. Let the player re-arrange their own items.

Or, at least, take out the rearrangement part and test the code so you can be sure the _only_ problem is the rearrangement part.

Or maybe none of this is good advice, I'm just guessing.

Posted on 2004-11-18 23:53:09

Technetium

Quote:Originally posted by Omni

Why do you need each general item structure to reference its own position in the player's backpack? That seems superfluous, and maybe cutting that would relieve a source of problems.

That's really hard to explain. Actually, it is a bit similar to how in Verge 1, there was an automatic party system. In that system, there was 'charatpos[]' and there was another variable that went the other way. One allowed you to find which character was at a specific position in the line, and the other allowed you to find where in the line a specific character was. It's the same here, basically. One tells you you where in the backpack index a specific item is, and the other tells you which item is at a specific location in the backpack index. The latter is necessary for drawing the graphics of the items in the inventory screen (so that it can run through a loop). The former is necessary for any sort of mega-loop that tests the location of all items in the game.

Other possibility: Don't shift items up or down in sequence when others are removed or added to the backpack. Let the player re-arrange their own items.

The rearragement is not in the position of the items displayed on screen; it's just the internal indexing. It's basically like if you have an array of numbers, and you want to delete one. Well, you would have to move all the numbers after it down to the previous index so that any for loops would still work right.

Or, at least, take out the rearrangement part and test the code so you can be sure the _only_ problem is the rearrangement part.

I've done loads of testing. Commenting out stuff, adding in more lines of logging than there are lines of regular code, etc. I've narrowed down the problem to less than 10 lines of code, and determined that it is most likely within 6 lines. But I have no idea what it actually is. I don't know if my code is having an unintended effect (such as using a variable that has been changed that I didn't know would be changed), or if it is just my limit of understanding where to go with it.

Posted on 2004-11-19 04:03:08

blues_zodiakos

Linked Lists might make this task monumentally easier. I think Freyr released a library for them a bit ago. You might want to look at that.

Posted on 2004-11-24 20:08:03


Displaying 1-4 of 4 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.