Search Unity

just another inventory

Discussion in 'Made With Unity' started by stneas, May 6, 2010.

  1. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Hi all,
    this was actually a part of my turnbased rpg prototype. Now when i know more about unity and c# i am rewritting whole thing, but made a rip of my inventory system. It contains a sample of integrated items database editor with saving to binary file and example of game inventory window.
    To use - unzip archive, load project, load 'test' scene, hit play. "I" key - brings up inventory menu,
    Q,W,E,R,T - add different items to inventory. Controls a similar to Dragon Age inventory, drag and drop items from inventory to slots and back, right mouse click on inventory item brings up item menu, where you can equip or destroy item.
    Note that this rpg project was started when i know little about programming, so there can be done a lot of optimisation, and a lot can be done better (what i actually doing right now :) ) but its works and i hope will be usefull to someone.

    P.S. Sorry for bad english in message and project commentary, but i think for unity community its better bad english than good russian :D
     

    Attached Files:

  2. AgainstTime

    AgainstTime

    Joined:
    Apr 14, 2010
    Posts:
    84
    amazing stuff out there man! Good job, *runs to check it out*...
     
  3. aksdad

    aksdad

    Joined:
    Apr 6, 2010
    Posts:
    36
    Wow! Thanks for this.
    I noticed you also threw in many more of you scripts like the player controller, game controller, message system etc.

    Thanks alot!
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    stneas:

    I'm not getting this to work for me.

    UnityPro 2.6.1f3 on the Mac

    I downloaded your project, loaded the scene test.unity from the project inventory. I hit play and type "i". I get the four buttons across the top, and the three empty slots for arms and armor. If I click on one of the empty slots, I get a screen full of NullRef! errors.

    The main error I'm getting on load is "No weapons file Found" from InventoryControl.cs and this seems to stop the loading process.

    There is a file called weapons.dat in the inventory folder outside the Assets folder.

    I did follow the logic a little, and InventoryControl.cs calls BinarySaver.Load(GlobalSettings.File_Weapons), and this is defined in GlobalSettings.cs as public static string File_Weapons = ".\\weapons.dat"; which is presumably correct...

    Any thoughts? Mac/PC thing?

    I'm also getting a small number of warnings that someVarible is assigned a value but that value is never used, but I think this is irrelevant to the issue.

    Anyone else having problems?
     
  5. kor

    kor

    Joined:
    Oct 29, 2009
    Posts:
    122
    Thanks! :D
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well...

    I've tracked the problem down to ( as I suspected ) loading the weapons.dat. I assume that this could be the same problem with the other .dat files. What I am not experienced with is all the serializable work and the .load ( and this doesn't seem to be resources.load ) that's being used. It's pointing to a string called .\\filename.dat ( and I've tried screwing with this to no avail ). What does .\\ indicate? Seems like it's trying to indicate a directory location?? What scares me is that this seems to be working for other folks out of the box, but not me.
     
  7. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Little Angel and all mac users, just get back home from work and try it on mac, yes seems the problem is in the ".\\" chars at the beginning of file names, in windows they represents current application directory and don't work on mac. Just removed then and left "weapons.dat" , "potions.dat", "armor.dat" and its starts working. About not used variables, as i said this was a part of my game prototype, i try to clear all unnecessary classes, methods and variables, but some might still stay.
     
  8. Ramen Sama

    Ramen Sama

    Joined:
    Mar 28, 2009
    Posts:
    561
    This looks intersting. i might learn something from it.
     
  9. platonia96

    platonia96

    Joined:
    Mar 10, 2010
    Posts:
    225
    This is exactly what i'm trying to do!
    Can I use these scripts, and maybe modify them too?
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Very nice.

    Works on the Mac when .\\ is removed.

    I see there is still some distance to go before it is finished.

    stneas: If you develop this, you can get revenue. Look at Aron Granberg's license for his A* pathing (www.arongranberg.com/unity/a-pathfinding) where the license states that you must pay if you are using the work in a commercial game.

    I, for one, would pay a reasonable fee along the lines of the fee for Aron's pathing system for a simple working inventory system if I were to use it in a commercial game.
     
  11. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Cool! Thanks so much for this! Exactly what I was looking for the other day.

    The side menu in the screenshot isn't there though. The one that says Item window, equip, destroy, cancel.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Just an FYI for those fooling 'round with this:

    The editor scripts for making items are not currently trapped, so if you hit "save database" and you have not yet loaded one, you will over-write your existing items database with a blank one.

    This is a bit cheap, but I put of a trap around the save button like this (from ArmorEditor.cs):

    Code (csharp):
    1. private bool wantToSave = false;
    2.  
    3.     void OnGUI()
    4.     {
    5.         GUILayout.BeginHorizontal();
    6.         if (!wantToSave) {
    7.             //  Load Database
    8.             if (GUILayout.Button("Load database", GUILayout.Width(128)))
    9.             {
    10.                 armor.Clear();
    11.                 Hashtable toLoad = BinarySaver.Load(GlobalSettings.File_Armor) as Hashtable;
    12.                 if (toLoad == null)
    13.                 {
    14.                     Debug.Log("No armor file found");
    15.                     return;
    16.                 }
    17.                 ICollection armorCollection = toLoad.Values;
    18.                 foreach (ArmorSaver listArmor in armorCollection)
    19.                 {
    20.                     Armor loadArmor = (Armor)listArmor;
    21.                     armor.Add(loadArmor);
    22.                 }
    23.             }
    24.        
    25.         //  SAVE Database
    26.             if (GUILayout.Button("Save database", GUILayout.Width(128)))
    27.             {
    28.                 wantToSave = true;
    29.             }
    30.         }
    31.        
    32.         if (wantToSave) {
    33.             //  REALLY SAVE Database
    34.             if (GUILayout.Button("SAVE DATABASE", GUILayout.Width(128)))
    35.             {
    36.                 Hashtable toSave = new Hashtable();
    37.                 foreach (Armor listArmor in armor)
    38.                 {
    39.                     ArmorSaver saveItem = (ArmorSaver)listArmor;
    40.                     if (!toSave.ContainsKey(saveItem.name))
    41.                         toSave.Add(saveItem.name, saveItem);
    42.                 }
    43.                 BinarySaver.Save(toSave, GlobalSettings.File_Armor);
    44.                 wantToSave = false;
    45.             }
    46.        
    47.         //  Cancel the save
    48.             if (GUILayout.Button("CANCEL", GUILayout.Width(128)))
    49.             {
    50.                 wantToSave = false;
    51.             }
    52.         }
    53.        
    54.         GUILayout.EndHorizontal();
    55.  
    56.         // Please continue with the existing code
    This just protects the save button with a required second click and changes the button position to prevent accidental double clicks.
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This also seemed to remove the chance of unseemly headaches.


    Code (csharp):
    1.     void OnEnable ()
    2.     {
    3.         LoadDatabase();
    4.     }
    5.    
    6.     void LoadDatabase()
    7.     {
    8.         armor.Clear();
    9.         Hashtable toLoad = BinarySaver.Load(GlobalSettings.File_Armor) as Hashtable;
    10.         if (toLoad == null)
    11.         {
    12.             Debug.Log("No armor file found");
    13.             return;
    14.         }
    15.         ICollection armorCollection = toLoad.Values;
    16.         foreach (ArmorSaver listArmor in armorCollection)
    17.         {
    18.             Armor loadArmor = (Armor)listArmor;
    19.             armor.Add(loadArmor);
    20.         }
    21.     }
    I've not thought the logic all the way through, but I can't seem to think of a reason not to load the database when enabling the editor script. This is just rearranging the code that was previously associated with the load button.

    I've also done this:

    Code (csharp):
    1. //  Load Database
    2.     if (GUILayout.Button("Re-Load database", GUILayout.Width(128)))
    3.     {
    4.         LoadDatabase();
    5.     }
     
  14. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Wow, looks good. I hope this can save me some time for save/loads, but I dont need any invatory, so I can just get rid of that.. but thanks, you may have just saved some people some time :D
     
  15. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    When I try to Export Package, it creates just a small file, not including most of the stuff, and then when I try to Import Package into another game I'm working on, it fails, claiming its because of the database.

    Any ideas why that happens?

    I could just copy everything over by hand I suppose. Just curious why that error would exist.
     
  16. aksdad

    aksdad

    Joined:
    Apr 6, 2010
    Posts:
    36
    Well, If you look at the scripts the save/load script wasn't made by him but was borrowed from some guy in the unity3d.ru forums.
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Isn't that what's so great about the Unity community? People post the work they've done and others improve on it and post it back. Just imagine how much work this would be if everyone had to make everything on their own. I work as a "one man band" right now, and the sharing and cooperation from the community was one of main reasons I chose Unity over the competition - and I'm so glad I did.

    Now I only wish I knew more languages - like Russian! So I could post my work and read the forums in other languages.
     
  18. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    aksdad : i'am not making big secret from that i borrowed the script, author commentary left untouched :D , for those who interested i also added to saveload system AllowAllVersionDeserializationBinder class from this article http://spazzarama.wordpress.com/2009/06/25/binary-deserialize-unable-to-find-assembly/ this was some serious pain to figure out why unity cannot load binary file when i making changes to inventory item classes, while xml files don't have this problem. Well at least for me this was serious pain :D

    Little Angel: yes, code is not foolproof :) , actually i use a little different save-load button methods using EditorUtility.OpenFilePanel and EditorUtility.SaveFilePanel. For example for load you can change code to :
    Code (csharp):
    1. if (GUILayout.Button("Load database", GUILayout.Width(128)))
    2.         {
    3.             fileName = EditorUtility.OpenFilePanel("Select armor db file", "", "dat");
    4.             if (fileName.Length != 0)
    5.             {
    6.                 armor.Clear();
    7.                 Hashtable toLoad = BinarySaver.Load(fileName) as Hashtable;
    8.                 if (toLoad == null)
    9.                 {
    10.                     Debug.Log("Error loading file");
    11.                     return;
    12.                 }
    13.                 ICollection armorCollection = toLoad.Values;
    14.                 foreach (ArmorSaver listArmor in armorCollection)
    15.                 {
    16.                     Armor loadArmor = (Armor)listArmor;
    17.                     armor.Add(loadArmor);
    18.                 }
    19.            
    20.             }
    21.             else
    22.                 Debug.Log("Error loading file");
    23.         }
    and have 'save' and 'save as' button, thus allowing saving a few versions of inventory db. Just wanted to keep code as simple as possible.
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Very nice!

    I'll have to look into that when I'm back at my desk. (I usually don't get time with Unity on Tue Wed.)

    And stneas: No criticism of code, especially code that is "work in progress". I just wanted to pass what I had discovered and post my solution code to the other people watching this thread. It took me a few time of deleting the database to figure out how I did it, so that was just to let people know in case they had done it as well.
     
  20. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oh, and YES!

    I'd be very interested in your AllowAllVersionDeserializationBinder class!
     
  21. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Little Angel: it's allready there :D , just want to add don't miss it if you just want add binary saveload script to your project, AllowAllVersionDeserializationBinder.cs also is in the SaveSystem, and it's used in load method of BinarySaver.cs
    Code (csharp):
    1. BinaryFormatter formatter = new BinaryFormatter();
    2.             formatter.Binder = new AllowAllVersionDeserializationBinder();
    3.             obj = (object)formatter.Deserialize(stream);
    Another solution probably will be to have all your serialised save classes packed to external dll, but that not convenient to me as using custom SerializationBinder;
     
  22. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Code (csharp):
    1. Assets/Scripts/Items/Weapon.cs(50,36): error CS0117: `WeaponSaver' does not contain a definition for `weaponType'
    Any idea why I'd have that error?

    I didn't change that script at all, nor the weaponseditor.cs

    I didn't have it before.

    Now it won't run, that not there.

    Also, the menu in the Unity editor that say "INVENTORY" is no longer there.
     
  23. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Wolf Dreamer: Check that WeaponSaver.cs have this line
    Code (csharp):
    1. public WeaponType weaponType;               //weapon type
    that error is as it's said emerges when class dosen't have definition for variable, or you could try restore WeaponSaver.cs from downloaded archive.
     
  24. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Weapon : InventoryItem
    4. {
    5.     public float minDamage;                 //weapon min-max damage
    6.     public float maxDamage;                 //
    7.     public GameObject projectile;           //weapon projectile instaniate on fire
    8.     public GameObject model;                //weapon model
    9.     public WeaponType weaponType;           //weapon type
    10.  
    11.     public Weapon()
    12.     {
    13.     }
    14.  
    15.     public Weapon(Weapon copyItem)
    16.     {
    17.         name = copyItem.name;
    18.         fullName = copyItem.fullName;
    19.         description = copyItem.description;
    20.         price = copyItem.price;
    21.         icon = copyItem.icon;
    22.         classRestriction = copyItem.classRestriction;
    23.         stackable = copyItem.stackable;
    24.         maxStack = copyItem.maxStack;
    25.         reqStrength = copyItem.reqStrength;
    26.         reqDexterity = copyItem.reqDexterity;
    27.         reqIntellect = copyItem.reqIntellect;
    28.         reqLevel = copyItem.reqLevel;
    29.         minDamage = copyItem.minDamage;
    30.         maxDamage = copyItem.maxDamage;
    31.         projectile = copyItem.projectile;
    32.         weaponType = copyItem.weaponType;
    33.         model = copyItem.model;
    34.     }
    35.  
    36.     public static explicit operator Weapon(WeaponSaver itemSave)
    37.     {
    38.         Weapon item = new Weapon();
    39.  
    40.         item.name = itemSave.name;
    41.         item.fullName = itemSave.fullName;
    42.         item.description = itemSave.description;
    43.         item.price = itemSave.price;
    44.         if (itemSave.iconName != "")
    45.             item.icon = (Texture2D)Resources.Load("ItemsIcon/" + itemSave.iconName);
    46.         if (item.projectile != null)
    47.             item.projectile = (GameObject)Resources.Load("Prefabs/Projectiles/" + itemSave.projectileName);
    48.         if (item.model != null)
    49.             item.projectile = (GameObject)Resources.Load("Prefabs/WeaponModels/" + itemSave.modelName);
    50.         item.weaponType = itemSave.weaponType;
    51.         item.stackable = itemSave.stackable;
    52.         item.maxStack = itemSave.maxStack;
    53.         item.reqLevel = itemSave.reqLevel;
    54.         item.classRestriction = itemSave.classRestriction;
    55.         item.reqStrength = itemSave.reqStrength;
    56.         item.reqDexterity = itemSave.reqDexterity;
    57.         item.reqIntellect = itemSave.reqIntellect;
    58.         item.reqLevel = itemSave.reqLevel;
    59.         item.minDamage = itemSave.minDamage;
    60.         item.maxDamage = itemSave.maxDamage;
    61.  
    62.         return item;
    63.     }
    64. }
    65.  
    No change there at all from the original.

    The line it points to is
    Code (csharp):
    1.         item.weaponType = itemSave.weaponType;
    I tried to add in a new type of item, but I didn't change the existing ones at all.
     
  25. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Wolf Dreamer: Check that WeaponSaver.cs in the Assets/Scrips/SaveSystem have weaponType variable, you showing Weapon.cs
     
  26. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. [System.Serializable]
    6. public class WeaponSaver
    7. {
    8.         public string name;                         //weapon name
    9.         public string fullName;                     //full weapon name
    10.         public string description;                  //weapon description
    11.         public int price;                           //weapon price
    12.         public string iconName = "";                //weapon icon resource name
    13.         public bool stackable;                      //if item unique or can stack for potions, spell scrolls and like
    14.         public int maxStack;                        //max items in stack    
    15.         public int reqLevel;                        //player min level to use item
    16.         public int reqStrength;                     //player minimum attributes required to use/equip item
    17.         public int reqDexterity;                    //0 - no requirement
    18.         public int reqIntellect;                    //
    19.         public CharacterClasses classRestriction;   //
    20.         public float minDamage;                     //weapon min-max damage
    21.         public float maxDamage;                     //
    22.         public string projectileName;               //weapon projectile instaniate on fire
    23.         public string modelName;                    //weapon model name
    24.         public WeaponType WeaponType;               //weapon type
    25.        
    26.  
    27.         public static explicit operator WeaponSaver(Weapon item)
    28.         {
    29.             WeaponSaver itemSave = new WeaponSaver();
    30.  
    31.             itemSave.name = item.name;
    32.             itemSave.fullName = item.fullName;
    33.             itemSave.description = item.description;
    34.             itemSave.price = item.price;
    35.             if (item.icon!=null)
    36.                 itemSave.iconName = item.icon.name;
    37.             itemSave.stackable = item.stackable;
    38.             itemSave.maxStack = item.maxStack;
    39.             itemSave.reqLevel = item.reqLevel;
    40.             itemSave.weaponType = item.weaponType;
    41.             itemSave.minDamage = item.minDamage;
    42.             itemSave.maxDamage = item.maxDamage;
    43.             if (item.projectile != null)
    44.                 itemSave.projectileName = item.projectile.name;
    45.             if (item.model != null)
    46.                 itemSave.modelName = item.model.name;
    47.             itemSave.weaponType = item.weaponType;
    48.             itemSave.classRestriction = item.classRestriction;
    49.             itemSave.reqStrength = item.reqStrength;
    50.             itemSave.reqDexterity = item.reqDexterity;
    51.             itemSave.reqIntellect = item.reqIntellect;
    52.  
    53.             return itemSave;
    54.         }
    55.  
    56. }
    57.  
    Didn't change that one either.

    Can you tell me the proper way to add in new elements? Because apparently whatever I'm doing is causing problems in unrelated scripts.
     
  27. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Here is you mistake, change
    Code (csharp):
    1. public WeaponType WeaponType;               //weapon type
    in WeaponSaver.cs to
    Code (csharp):
    1. public WeaponType weaponType;               //weapon type
    variable and enum type have the same name, upper and lower case matters.
     
  28. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    :eek: Yeesh! How'd I miss that? I thought I checked it.

    Anyway... got other errors now.
    Instead of just armor, weapon, and potion, how do I create something else?

    I copied ArmorEditor.cs, Armor.cs object, and ArmorSaver.cs to Button1Editor.cs, Button1.cs, and Button1Saver.cs, and then edited those three new files, changing the name armor to Button1. Anywhere else I found it calling any of those armor files(in the same places it also called WeaponEditor, Potion.cs, etc) I copy and pasted what it listed for armor, and then changed the word armor to Button1.

    I'd like to have it where I can label things for the head, feet, legs, arms, torso, hands, etc, as well as have it where I can drag something from my skills/spells list to lock it to an area I create at the bottom for it. Be able to fire spells and attacks that way. Any suggestions?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class InventoryControl
    6. {
    7. private List<InventoryItem> InventoryItems = new List<InventoryItem>();
    8.  
    9.  
    10. public void LoadItems()
    11. {
    12.     //Load weapons
    13.     Hashtable toLoad = BinarySaver.Load(GlobalSettings.File_Weapons) as Hashtable;
    14.     if (toLoad == null)
    15.     {
    16.         Debug.Log("No weapons file Found");
    17.         return;
    18.     }
    19.     ICollection itemCollection = toLoad.Values;
    20.     foreach (WeaponSaver listWeapon in itemCollection)
    21.     {
    22.         Weapon loadWeapon = (Weapon)listWeapon;
    23.         InventoryItems.Add(loadWeapon);
    24.     }
    25.     //Load armor
    26.     toLoad.Clear();
    27.     toLoad = BinarySaver.Load(GlobalSettings.File_Armor) as Hashtable;
    28.     if (toLoad == null)
    29.     {
    30.         Debug.Log("No armor file Found");
    31.         return;
    32.     }
    33.     itemCollection = toLoad.Values;
    34.     foreach (ArmorSaver listArmor in itemCollection)
    35.     {
    36.         Armor loadArmor = (Armor)listArmor;
    37.         InventoryItems.Add(loadArmor);
    38.     }
    39.     //Load potion
    40.     toLoad.Clear();
    41.     toLoad = BinarySaver.Load(GlobalSettings.File_Potions) as Hashtable;
    42.     if (toLoad == null)
    43.     {
    44.         Debug.Log("No potion file Found");
    45.         return;
    46.     }
    47.     itemCollection = toLoad.Values;
    48.     foreach (PotionSaver listPotion in itemCollection)
    49.     {
    50.         Potion loadPotion = (Potion)listPotion;
    51.         InventoryItems.Add(loadPotion);
    52.     }
    53.         //Load Button1
    54.     toLoad.Clear();
    55.     toLoad = BinarySaver.Load(GlobalSettings.File_Button1) as Hashtable;
    56.     if (toLoad == null)
    57.     {
    58.         Debug.Log("No Button1 file Found");
    59.         return;
    60.     }
    61.     itemCollection = toLoad.Values;
    62.     foreach (Button1Saver listButton1 in itemCollection)
    63.     {
    64.         Button1 loadButton1 = (Button1)listButton1;
    65.         InventoryItems.Add(loadButton1);
    66.     }
    67. }
    68.  
    69. public InventoryItem FindItem(string itemName)
    70. {
    71.     InventoryItem itemFind = InventoryItems.Find(delegate(InventoryItem item)
    72.     {
    73.         if (item.name == itemName)
    74.             return true;
    75.         return false;
    76.     });
    77.     if (itemFind != null)
    78.     {
    79.         if (itemFind is Weapon)
    80.         {
    81.             Weapon retItem = new Weapon((Weapon)itemFind);
    82.             return retItem;
    83.         }
    84.         if (itemFind is Armor)
    85.         {
    86.             Armor retItem = new Armor((Armor)itemFind);
    87.             return retItem;
    88.         }
    89.         if (itemFind is Potion)
    90.         {
    91.             Potion retItem = new Potion((Potion)itemFind);
    92.             return retItem;
    93.         }
    94.               if (itemFind is Button1)
    95.         {
    96.             Button1 retItem = new Button1((Button1)itemFind);
    97.             return retItem;
    98.         }
    99.     }
    100.     return itemFind;
    101. }
    102.  
    103.  
    104. }
    105.  
     
  29. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    Wolf Dreamer: for cast error, check all explicit conversions in button and buttonSaver, when copy-past code is most probably you miss something when replacing armor with button. For differnt slots is better create Slot class, something like:
    Code (csharp):
    1. public class Slot
    2. {
    3.     public Rect slotRect;
    4.     public InventoryItem slotItem;
    5.     public SlotType slotType;
    6. }
    then create array in character class:
    Code (csharp):
    1. public InventoryItem[] inventory = new InventoryItem[10];
    define constants to use instead numbers:
    Code (csharp):
    1. public const int head = 0;
    2. public const int leftArm = 1;
    3. public const int rightArm = 2;
    4. etc,etc...
    5.  
    so you can access your slots like this:
    Code (csharp):
    1. item = inventory[head];
    and use for..each statment for inventory when drawing interface and check for drag and drop.

    To check that items drops in their slot, you can create SlotType enum and add it to Slot class and InventoryItem class and simple check that item.slotType == slot.slotType
    Code (csharp):
    1. public enum SlotType
    2. {
    3.     Head,
    4.     Arm,
    5.     Leg,
    6.     Ring
    7. }
     
  30. Neonix

    Neonix

    Joined:
    Nov 28, 2009
    Posts:
    1
    Wow .. good job with this code mate. I was looking for a tutorial on how to use enums but this is 500x better than anthing I found.

    Must feel good to inadvertantly be teaching so many others while your trying to learn, eh? hehehe

    :D
     
  31. larrys

    larrys

    Joined:
    Nov 6, 2010
    Posts:
    1
    How can I replace a defaultSkin Editor scripts for u3

     
  32. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    hey, whats best ever (what i sow) inventory sytem . good work.
     
  33. stneas

    stneas

    Joined:
    Sep 25, 2009
    Posts:
    9
    larrys: replace it with
    Code (csharp):
    1.  private GUISkin defaultSkin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector);
     
  34. wwy

    wwy

    Joined:
    Oct 4, 2010
    Posts:
    2
    private GUISkin defaultSkin =GUIUtility.GetBuiltinSkin(1);

    Assets/Scripts/Global/GameControl.cs(26,41): error CS0122: `UnityEngine.GUIUtility.GetBuiltinSkin(int)' is inaccessible due to its protection level

    why??
     
  35. TvM79

    TvM79

    Joined:
    Nov 26, 2010
    Posts:
    53
    Might cause other issues, but try to set it to public GUISkin
     
  36. Tharkis

    Tharkis

    Joined:
    Mar 7, 2012
    Posts:
    16
    Sorry to necro this post, but I found this code and wanted to play with it a bit. However, it looks like some methods that are used in this code are not available in the latest version of unity. Unity throws an error at this line: private GUISkin defaultSkin = GUIUtility.GetBuiltinSkin(1);

    As I understand it GetBuiltinSkin() no longer exists. Is there a way to fix this without having to refactor all the code?