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

Inventory system - From scratch, need tips etc

Discussion in 'Getting Started' started by Loff, Jan 22, 2015.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hey!

    In past projects I have never finished a inventory system because the tutorials I was following and because for example the "teacher" stopped uploading new videos. I do understand the script they made, but in the end I either don't like it or it's missing a lot of features I don't know how to handle.

    So what I would love is ideas around how to:
    - Store sprites and item info (database (?)) . Prefer not to add sprites and such to objects (public ..) in case it gets lost and I got to add every sprite again. Spreadsheets would be nice, but single sprites are fine!
    - Draw and use inventory
    - General stats/item info and colored text on mouse over
    - Right click menu
    - Equip and equip inventory
    - Vendor (?)

    Before I start googling as earlier, trying my own scripts and others.. I would love to hear from you guys!
    I have been working a bit with arrays because that seems pretty handy for inventory systems, but other things would be awesome. Not good with loops, like for drawing.
    Anyhow, all info is welcome and I mean everything. I hope to get some responds to the thread and not only 1 person saying for example "array" and then it's dead.
    Hope to hear from you guys, I know it's a good community.
    Feel free to add links as well :)

    Cheers!
     
    Last edited: Jan 22, 2015
  2. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    When I said "I hope to get some responds to the thread and not only from 1 person". I was hoping to get more then 0 at least :D *bump*
     
  3. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Generally when you post on a topic that can be as large and complex as an inventory system you won't get many responses because the complexity of the task discourages discussing such complex designs. Especially in a game creation forum. 'Inventory' is not seen as sellable as 'Shooting' and so doesn't draw as much interest.

    If you want design tips you can search for example designs common on the internet and implement them, e.g. the scientific naming system, Wal*Mart's, Amazon's and many more have very good inventory.

    As a programmer I can tell you Amazon makes an very good inventory design and implementation available to the public free of charge. You'd need to repurpose it to be useful in a game of course and when I say it's complex, it is. And it's also extendable of course.

    There are also several free turnkey 'shopping cart' systems available to the public for free implement in various languages. The game developer need only port to be suitable for the game engine they are using.

    Good luck.
     
  4. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    I agree. I don't look for a complete answer by someone as it's a big task. I don't think Amazon/Walmart etc is the kind of inventory that's gonna help me too much. It's not like a store inventory, but of course it has some of the same functions. Thinking about starting creating a database for items and sprites followed by creating a drawing loop for inventory and then the items placed at each slot. Not really sure how to do that yet. I know Array, but the rest. Will start work on it tonight :)
     
  5. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    I wrote an inventory system a while back before NGUI 3.0 and uGUI.


    Right as I began selling it, NGUI 3.0 came out and broke it... mine works with 2.0

    I would re-write the system now...but the initial one took a long time to write and debug...
    My own advice:

    - Please for the love of Christ, make the system use slots... not just one slot per item like most inventory systems on the asset store. This allows users to either use one slot per item or multiple (see my example above).

    - I "filled" the inventories as 2D boolean arrays... whether this is a good way to do it is up for debate.

    - The new Unity GUI is heaven for sprite management - use it for item sprites.

    - If you decide to make a multiple-slot per item type of inventory, make the sprites re-sizable

    - The biggest hint I can give is to really flesh everything out before you start, that way you have a good direction to go in. If you add features to the inventory as you go, it gets complex quickly and you end up with spaghetti code. This means narrowing down which features you are going to support like stackable items, item weights, etc.

    My brain is in a fog right now... If you want some ideas as a basis for your own system, I can give you the code from mine if you are interested (just PM me).
     
    Loff and goat like this.
  6. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    There are several free Unity based inventory systems available if you want to Google them. nGUI Free, if it's still available, has one.
     
  7. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    I like what you wrote, makes me thing about how and what to do! Feels like it's a bit over my level however, just the drawing of the inventory, grabbing items from database, right clicking for options etc is a bit scary :) sounds overwhelming if you know what I mean. The video you showed is private btw, would like to watch it!
    I don't mind getting the code you wrote either, but I'm not sure how much of it I would understand if it's really complicated as most inventory scrips are
     
  8. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    Ok, check the link again... sorry about that. I forgot I made it private
     
    Loff likes this.
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    A basic inventory system is not trivial. A really good inventory system takes a lot of work and a good optimized sustainable transportable inventory system is a lot more work.

    I'd google/search (there is a button at the top of the page) "Inventory" and see what people have done. There are several, if not many, threads on the forums about making inventory systems.

    There are also many different approaches that work for different types of games - that require different techniques.

    Single slot based, stackable (WoW-like) inventories are very different in their issues and solution from a grid-based inventory. Some decisions need to be made re: bags and bag count, encumbrance, etc. - and how integrated your system is to your game. They can become terribly tangled and dependent when you get into attributes, quests, crafting... all of which are systems that interact with the Inventory system, but shouldn't be tangled up with it. Keeping your systems separated and dependency free is tough.

    Some hints:

    • Look into scriptable object for data management.
    • Look into editor scripting to make tools to help manage your items.
    • The new UI system is very helpful when it comes to managing sprite assets, but, as a "Retained UI" can be more painful to code than an "Immediate Mode" UI when it comes to assigning "slots" from an inventory array to persistent, scene based sprites. Just be prepared for some hoop jumping keeping persistent sprites updated with dynamic data.

    If you want to start a thread that is not in the "getting started" thread, but in a more appropriate one - like Game Design (http://forum.unity3d.com/forums/game-design.71/), I'll give you a hand, as I've made a few of these.
     
    Ryiah and Loff like this.
  10. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Thanks for taking your time!
    Will check out the ideas you are talking about, thank you so much :)

    Started working on my inventory yesterday. It's really not a good code, but it can work. I might try to finish this and then I see how it went before I re'scripting or make it different. Maybe I should read about data management etc first so I don't have to redo everything if what I make is really bad. However.. in the end it will take a lot of time just making it better, but just getting a starting position is worth a lot to me!

    The reason I posted it inn "Getting started" is because another thread I made earlier was moved to that section, and I thought this was very close to the same, so I tried posting it here. However I would normally never make a thread in this section as It would fit better inn "Game Design" for example.

    Cheers!
     
    Last edited: Jan 27, 2015
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Don't worry about perfect. I've made at least 3 iterations on a slot-based inventory system.

    The first one was simply an array (tho' now I'd use a generic list) of the type "InventoryItem".

    I had created a class called "InventoryItem" to hold the representation of my Inventory Item, with a:
    Code (csharp):
    1. public string name;
    2. public Texture2d itemIcon; // this could be a ... sprite or other image type now...
    3. public Rigidbody itemModel; // identified by it's rigidbody, so it could be instantiated and fall down in the scene...
    4. public OtherThings likeWeight; // etc.
    I had enums like:
    Code (csharp):
    1. public enum SlotType {None, Hand, Bag, Head, Chest, Ring, Ear, Nose, Etc}
    ... so I could test what type of thing it was against a character slot...

    ... but I'm sure you'll have fun in any case.

    If you get stuck, start a new thread in the appropriate forum and PM me the url and I'll go have a look.
     
    Loff likes this.
  12. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Sounds good! Thanks :)