Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Logic for inventory system with various size items (Diablo 3)

Discussion in 'Scripting' started by kubajs, Mar 7, 2018.

  1. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    57
    Hi guys,
    I'm trying to figure out the best approach (high level - just the rough logic behind, no code needed) for handling inventory allowing various size items (very similar to inventory in Diablo 3). Some items should occupy 1 slot, some 2+ slots.
    I tried to go with simple 2 dimensional array but I expect problems during implementation of drag&drop for multi slot items later.
    My first approach was
    - 2 dimensional array,
    - each item's image consists of array of sprites (or one splite which can be splitted to multiple sprites programatically). I can use the same approach for slot background.
    Using this way I'm able to do the task roughly but I feel it's not ideal way and might get pretty complex later.

    Another idea was to use list and various size blocks and connect adjacent items using "pointers" or something so each block would know what are it's horizontal and vertical neighbours.

    Any idea appreciated.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I've never done this but splitting sprites sounds logical to me. Some extra adjustment work would be required if the player can resize the width of the inventory, I imagine.
    Items would normally be a little less wide and a little less tall than their grid cell. Larger items would be a little less wide and a little less tall than 2+ cells.

    I think you could just "infer" other occupied cells, if you consider the item placed in.. say the top one but it's "2 tall", you can know it's also below if you store the top one -- if that makes sense. That's just thinking out loud. :)
     
    kubajs likes this.
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I don't know much about it, but you might want to study layout groups for the UI and see if there is some kind of auto adjustment for sizes.
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,877
    I think your overthinking this:

    What you essentially need is a grid of 1x1 squares that doesnt care about what data is coming into it, only cares about fitting stuff into itself.

    So before you even start worrying about the item part, make a grid behaviour that displays itself, and then write a "grid object" class of some kind that has a "size" property

    The size can be 1x1, 2x2, 2x1, 3x3 etc etc whatever you want. For now for ease of use store this however you want, you just need to be able to track what item is made up of how many grid squares.

    Now in your grid behaviour, write a function that takes in a list of "grid objects", loops through them, checks their grid size in terms of how many boxes they take up, and then attempts to fit them into the grid.

    If your able to do that then the rest is easy, there are 100 of tutorials on inventory systems.

    So to recap:

    1. Seperate the "grid + grid placement" logic from the inventory logic
    2. Get your grid placement logic working with the grid objects. If you can place a set of 4x4 , 3x1 and 2x2 objects for example in a grid made up of 20x20, then the rest of this will be easy
    3. Make a standard inventory using a tutorial, perhaps a scriptable object format or similar.


    http://catlikecoding.com/unity/tutorials/procedural-grid/

    This tutorial will show you how to make a procedural grid



    https://forum.unity.com/threads/tut...pg-inventory-system-in-unity-and-unet.495021/

    That tutorial will get you started but the general structure i proposed will work and is simple enough to program.
     
    kubajs likes this.
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just for the record, I did assume that a grid layout would be used. The built-in UI component. :)
     
    MadeFromPolygons likes this.
  6. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    57
    @Daemonhahn This is exactly what I came with after thinking deeper about the problem. Yes it's necessary to separate the grid from the items / physical slots with items and use the grid only for tracking purposes. Shouldn't be very difficult to do.
    Thank you guys!
     
    MadeFromPolygons likes this.