Search Unity

Better way to handle massive item database?

Discussion in 'Scripting' started by xXSLUGSXx, Oct 23, 2014.

  1. xXSLUGSXx

    xXSLUGSXx

    Joined:
    Dec 26, 2013
    Posts:
    5
    I am currently using lists to create an item database, though I do not have many items in now (21), depending on how carried away I get with my game, I can see this number growing to 400 - 600 max. Before I start plowing out a bunch of list entries are there any options more efficient than a large unity list? Currently each item holds the following values, may add a few as I go.

    Name(String), Item Description(String), Item Weight(Float), ItemStacks?(bool), StackAmount(int), Equippable?(bool), EquipSlot(String), ItemQuality(String), Carry Weight Boost(int), ShotsLeft(int), ItemType(string), Items In Stack(int).

    Currently 14 Values per entry, meaning 5600 to 8400 values total if list becomes large enough.
     
    DylanYasen likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I find the Unity inspector's list editor to be a little cumbersome for more than a handful of items. I would probably define my items in a file instead (using GRFON, but pick your own preference), and then read these in on Start.

    Or, if you prefer to work entirely within the Unity IDE, you could consider making your own custom editor.
     
  3. xXSLUGSXx

    xXSLUGSXx

    Joined:
    Dec 26, 2013
    Posts:
    5
    I am actually writing the database in c#, the inspector was def. too much and gave great risk of losing everything if I accidentally deleted the game object the script was attached to. Heres an example, I defined the Item class in another file.
    Code (CSharp):
    1.         items.Add (new Item (
    2.             //Name(String)
    3.             "Old Car Battery",
    4.             //ID(Int)
    5.             20,
    6.             //Description(string)
    7.             "An old car battery",
    8.             //Item Weight(float)
    9.             30.0f,
    10.             //Carry Boost(float)
    11.             0.0f,
    12.             //Stacks(bool)
    13.             false,
    14.             //StackAmount(int)
    15.             1,
    16.             //Equippable(bool)
    17.             false,
    18.             //EquipSlot(string)
    19.             null,
    20.             //Quality(string)
    21.             " ",
    22.             //Item Weight Capacity If Container(int)
    23.             0,
    24.             //ShotsLeft(int)
    25.             0,
    26.             //Item Type(string)
    27.             " ",
    28.             //Items in Stack(int)
    29.             1
    30.             ));
    The list class uses [System.Serializable], I think this would be the same as the GRFON u mentioned?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well it's functionally the same. It's not the same experience for whoever has to write/maintain all that stuff (in particular, the values are unnamed, leading to the need to include all those comments.) But it's certainly not a bad way to go.
     
  5. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    ScriptableObject's, anyone?
    Create a class hierarchy which reproduces properties of your items and simply store instances of those as assets within your project.
    Thats effectively what im doing in my mmo, easy to maintain and to extend.
     
  6. Deleted User

    Deleted User

    Guest

  7. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    I'm very interested in this topic. Hope more people can come to discuss.

    The way I'm handling the item system in my current project:
    well at first, I was keeping everything in the code.

    Like some people have mentioned above.
    In order to let non-programmers to be able able to add new items, I made the class serializable so they can modify it from the inspector.

    But I'm still worried about some day, some one will mess up and “accidentally” deletes all we had, and applies changes to the prefab.

    I really want to learn a better way to deal with item database in terms of efficiency, maintenance and extendibility.

    Please share your thoughts. Thank you
     
  8. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Either use custom text/xml/binary files are simply ScriptableObjects. You'll be able to save every single item as a seperated file. No random deleting of everything and uses the conveniency of the project view plus inspector. What else would one want. :D
     
  9. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    I was working on this some last night, actually. I've teetered between a few things, and I'm currently using a single ItemDatabase ScriptableObject file with a list of all items. The item is saved to an Asset file. Then I have a custom editor window that allows me to easily add, modify, or delete items, and update the asset file. When the game starts, the items are loaded from the asset file.

    I considered a ScriptableObject for each individual asset, but I found it easier to modify the overall database on the fly rather than find the correct asset file in a large list. I am implementing sorting and searching through the editor window now so I can easily lookup items by specific fields.

    Loading from an outside file is nice too, but the benefits of the method I used is that it is easy to link in more complicated fields, like sprites, directly in the window. I couldn't do that as easily outside of a Unity editor.
     
    JoeStrout likes this.
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    This format makes me immediately think "spreadsheet". One row per item, compact, easy to read and edit. MS Excel, Open Office, Google Sheets, whatever. You can export to a CSV (comma-separated values) text file and read it into Unity with very little effort.
     
  11. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If you're worried about losing work, use subversion or git or ANY kind of version tracking!

    It's like having undo for everything. It's nice to be able to tag a build before I do major refactoring, and there have definitely been times were I screwed up things so bad that the best option was just going back to how the project was yesterday.
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Everyone should be doing this anyway. Bitbucket is free and private so there's really no reason not to.
     
  13. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    Please let's don't go side track here.
    version control is a good topic too.
    But let's focus on item system. shall we
     
  14. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    I am currently using XML myself, with a custom written editor window for a nice interface to define new item, assign meshes, icons and other properties, then save back to the XML file. The files get loaded in Start and a List of Item's populated. I might consider switching to JSON or something similar (less overhead data), or even go full binary since I have an editor anyway.
     
    DylanYasen likes this.