• 13
• 27
• 9
• 9
• 20

# Rpg Inventory gui

This topic is 774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,
I need a little idea about rpg inventory gui system. I have an inventory system in my game which works like most 2D browser based mmorpg. Single item occipies single slot. What I want to do is, make it more like diablo inventory system. Different items occupies different slot(s). I need some idea how to approach? I only need to know how do I detect the slot size of an item over inventory slots. My inventory right now is more like the left one, I want to convert it to right one (Diablo).

http://pasteboard.co/1cfK491f.jpg

##### Share on other sites

What game engine are you using?

##### Share on other sites

Not sure I understand you correctly, I'll rephrase the question for verification.

You want to have a different slot size for each item.You put them from left to right (for example) in the inventory gui.

Now you wonder when the user clicks at an item, which item should be used?

I think a binary search would work https://en.wikipedia.org/wiki/Binary_search_algorithm

You make an array, where you put the horizontal start position of each item from left to right. That should give you a sorted list numbers.

When the user clicks at the inventory gui, at the item row, you take the horizontal position, and look up the number left of it (there is an edge case of clicking at the border between two items, which you may want to look into).  That search gives you an index in the array, which links back to the n-th item you put in the inventory.

##### Share on other sites

I am using irrlicht graphics engine.

Let me explain it. Say, I have an inventory of 6x7 tiles (32x32 pixels each tile). Like this

[[0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0]]


Now I have a helmet which is 2x2 tiles (64x64 pixels).

[[1, 1],

[1, 1]]


How do I calculate helmets positions in inventory?

##### Share on other sites

Oh, not 1D, but 2D positioning. I see!

It's a multi-dimensional knapsack problem, which is known to be NP-complete, ie brute-force computing is not going to work for a sufficient number of items that should be placed.

The http://en.wikipedia.org/wiki/Knapsack_problem page discusses it, and probably gives some algorithms to approximate a solution as well (didn't read the entire page).

I recently had a button placement problem at such a 2D area recently, which I solved by

1. Fill from high buttons to smaller buttons (height was the biggest problem in my case)

2. Fill from top to bottom.

More concretely:

1. sort buttons on decreasing height;

2. keep track of the highest free row at each column.

3. for each button:

find a start and end column for the button, such that it is as high as possible

The idea was that the big ones leaves holes, which the small buttons can fill later. For my button problem this worked, but it is highly dependent on the shape of the objects.

I guess you should do some experiments to find out which algorithm works best for you (and if not, tweak it until it works).

Edited by Alberth

##### Share on other sites

As far as I can tell, in Diablo 3 there's a distinction between how small items (e.g rings and gems) and larger items are placed. The small items are placed left to right and seem to fill up columns first. Larger items are also placed left to right, but fill up rows first. In addition to that, the larger items are placed so that they don't waste rows (i.e. it tries to avoid having a single almost empty row). I think they've made their lives easier by having only two sizes, 1x1 or 1x2. What are the different shapes and sizes in your game?

##### Share on other sites

I am thinking more like diablo 2. different sizes for different items, from 1x1 to 2x6.

I will do the experiments. Thanks for the replies.

##### Share on other sites

This is only a theory... but perhaps you can try an algorithm that uses a general rectangular representation, in which the computer tries to calculate the area for?

So... essentially, You have a set of rules that determines where larger objects are allowed to be place first. As you add more, you build the tightest fitting rectangle based on all contact points of each smaller rectangle.

You check each possible position for the most efficient placement, which is the general rectangle with the smallest area. This might be a brute force... but its a start.

The game might also have to keep track of larger free clusters.

For instance... if a section of free spaces exists at coordinates (5,2),(6,2),(5,3),(6,3). Then you have an effective empty rectangle of (5,2),(6,3)

Which is calculated as a 2x2 box.

You might also need a way of saying that there is an adjacent empty rectangle nearby. In the long run, that might make it easier to determine if an item can fit in a location or not if the user manually trys to drop something in.

Edited by Tangletail

##### Share on other sites

I am still in theory. I will code some tests and see how it comes out. Thanks for reply.

##### Share on other sites

Diablo 2, path of exile, etc..., the inventory placement isn't very 'smart'. It probably isn't going to, by default, pack the items most efficiently. Especially since the player controls the order in which they are placed, and the optimal would require rearranging previously placed items. You can make the assumption that players are going to have to perform some manual inventory shuffling; that's actually a (small) part of the genre. It presents them a small optimization problem they need to tackle as part of their decision making. Can I fit more stuff? Do I need to portal back to town now? With that in mind, it's quite simple to just start at the top left corner and work your way down the column. Test to see if the item can fit right there (ie, are there enough empty squares to the right and below it to fit the item?). A brute force test is sufficient. If no spot is found, start at the top of the next column and work downward. If you reach the last column, and find no space for it, you play the annoying "I have no room" voice effect and drop the thing back on the ground.

This simple method should work well enough.  It will result in, for example, a 2x3 piece of armor being placed preferably in the top 3 rows of the inventory, and if a 2x2 helmet is picked up, it will end up in the 2 rows beneath it (if empty). 1x1 objects will 'fill in' the empty spaces from left to right. Are there better ways? Maybe. Doesn't matter. IMO, you can meet player expectations by simply emulating the games that came before.