Search Unity

Creating an Inventory

Discussion in 'Scripting' started by phoen, Jan 2, 2007.

  1. phoen

    phoen

    Joined:
    Oct 25, 2005
    Posts:
    94
    Im trying to create a simple inventory.
    Creating an array and adding new items seems to be a good solution.
    New items get put into the next free slot. There should be no gaps when items are taken out of the inventory.

    How should I approach this?
    Maybe someone has a few hints.
     

    Attached Files:

  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I'd start with an array of items. Display, say, 4 items in each row (like in your picture). Then if an array has 9 items, it will be two full rows and one item in another row.

    Adding a new item is just appending to the array. Removing an item is shifting all remaining items one index back. Both are probably easiest to do using javascript arrays
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I have a working inventory that works just like that in BotBuilder. What I did:

    1) created an array of "libColliders" representing all possible spaces to fill. These contain a script that uses OnMouseOver/etc to set an internal variable, retrieved through a wasChosen() function. This array is a builtin array.

    2) in the master script, create a seperate array using the Array class representing all items in the inventory. Add a helper function to add and remove these items. (you can represent items however you like; in my implementation, the items in the array point to objects in my Resources folder.)

    3) In Update, loop through the array created in #1 and check each object's wasChosen().

    That's the jist of it. I've included my implementation for reference/inspiration; depending on the internals of your game you'll probably want to write yours from scratch though. There is no Remove function in mine as it's not needed in Botbuilder, but it shouldn't be hard to add.
     

    Attached Files:

  4. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  6. phoen

    phoen

    Joined:
    Oct 25, 2005
    Posts:
    94
    Thank You guys!

    StarManta, i think your script (although i dont understand everything in it yet)gives me a good point to start. I always tried to avoid getting too close code, but now i actually start to enjoy reading through these scripts, linking the pieces together.

    You used a Mathfx function from the wiki. I have put that script (Mathfx) )into the Standard Assets folder to make it work. Am i wrong?
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The script only uses Mathfx.Berp for animation of the scrolling; it's really not needed. In fact, most of the second half of that function is superfluous eye candy.

    That said, Mathfx is incredibly handy. To use it, it goes into the Plugins folder.

    Oh, and I'm glad you found it handy :)