Search Unity

RPG Inventory Source

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

  1. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    This Inventory system is now community project to create a full featured mmorpg inventory system!

    WEB DEMO - Not up to date
    www.ruinati.com/Inv


    If you use this keep in mind I as a person am not a programmer, nor am I an artist or anything for that madder, I am just very ambitious and love to create games and share stuff with the lovely Unity Community.
    That being said my code may be VERY sloppy and uncommented, the models and everything are just place holders for you to replace.
    I decided to throw the source code out there because lots of people were asking about it, this may not be the best way to make an inventory but least now that it is out there hopefully I can get feedback on the code and everyone can work together to better it and perhaps make a solid inventory system.
    That being said there is one rule to using this code:
    TERMS OF USE:
    If you use this code and change it for the better, post the updated source back in the thread so we can better this project together.
    Its free to use in any commercial or non commercial game as long as you abide by the first rule.

    I hope this list is still correct :

    Finished Features:
    Inventory toggle
    Easy object creation workflow
    Easy pick up and parent system
    Easy inventory layout
    Left click grab and move items around
    Right click Equiping
    Right click open bag
    Bags carry items and have their own array
    Bags with items cannot be put in other bags
    Open all bags - Shift Right Click
    Tooltip to show item statistics
    Tooltip repositioning
    Shift Hover Tooltip comparison
    Smart pickup

    Features to come:
    Money - Finished, not in current web version
    WASD Movement right click first person look - Finished, not in current web version
    Bag X button - Finished, not in current web version
    Stackable Items - Finished, not in current web version
    Shift right click stack select - Finished, not in current web version
    Useable Items - Finished, not in current web version
    Banking Items - Finished, not in current web version
    Trading Items
    Dropping Items - Finished, not in current web version
    Destroying Items - Finished, not in current web version
    Buy/Sell Items
    Loot Items off corpse - Finished, not in current web version
    Better bag positioning
    Optimisations
    Visually appealing
    Shortcut Keys
    Suggestions/Requests


    Anyway I hope you all like the source and all feedback/ bug reports are welcome always.
    Enjoy.
     

    Attached Files:

  2. sciguy77

    sciguy77

    Joined:
    Nov 1, 2009
    Posts:
    191
    Really nice work, so far I can't find anything wrong with it (other than that you may want to improve the visuals in the future).
     
  3. jcullen24

    jcullen24

    Joined:
    Feb 27, 2010
    Posts:
    116
    Really really nice!


    Jim Cullen
     
  4. wizardious

    wizardious

    Joined:
    Dec 31, 2009
    Posts:
    189
    Very nice! :D Seemed to work perfect.
     
  5. Cyril

    Cyril

    Joined:
    Mar 19, 2010
    Posts:
    24
    Good work!
    but just a little suggestion!
    would be better to seperate 4 buttons outside of the inventory for 4 more bags instead.
    like in WoW
    else u have to open the inventory first before u can
    access the other bags and shortcut keys for the diff bags is a must be!
     
  6. AgainstTime

    AgainstTime

    Joined:
    Apr 14, 2010
    Posts:
    84
    Looks very good!
    looking forward to get updates from you, also it'd be amazing if u could release the source code, ty in advance.
     
  7. perversonality

    perversonality

    Joined:
    Jan 8, 2010
    Posts:
    57
    One slight crit which is that the bags should probably close automatically when the main inventory window is closed too or you need to add a close option for each bag but it's working very well
     
  8. Sarper-Soher

    Sarper-Soher

    Joined:
    Mar 10, 2009
    Posts:
    799
  9. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    I pushed out a new web build and added:
    tooltip repositioning
    shift hover equipment tooltip comparison
    smart shift right click equipping
    function feature smart shift right click toggle all bags
    smart item pickup

    They weren't my main concern but you are right and I will be improving them in the future.

    I have that tutorial yes, I never got the hole way through.
    It did give me a general idea of how to make an inventory.
    I did use a few more resources on the forums:
    http://forum.unity3d.com/viewtopic.php?t=46222&highlight=inventory
    http://forum.unity3d.com/viewtopic.php?t=11865&highlight=inventory
    but mostly I just started from scratch and learned as I went.
    Some of the functionality in my inventory I'm not sure is in the tutorial.
    The way my inventory is set up is you can add objects very easily to the game including backpacks/bags.
    This way your inventory can expand with the acquisition of more backpacks/bags and you aren't limited by any factors.
    Everything in the inventory is highly customizable with just a few lines of code or changing values in the inspector.

    The bags can easily be positioned anywhere and taken outside the inventory toggle if statement.
    However I wanted to go with the Ever Quest type inventory where you open it up to see what is inside.
    I will be adding shortcut keys for the bags and an exit box in them.
    Any suggestions on what the keys should be for the bags?

    I actually intentionally made the bag functionality like that, this way you can close out your inventory and have more room space if you wanted to do something while still having your bags open. I will add a button to close to bags individually.
    This however can easily be changed as all I need to do is drag 8 lines of code from beneath the if inventory toggle statement into it.



    Anyway ill keep adding new features and please don't be shy
     
  10. Tobe_sta

    Tobe_sta

    Joined:
    May 8, 2010
    Posts:
    20
    Really nice work mate!

    I've got a quite similar inventory to yours, except it's more of a "backpack" inventory rather than your "character" one but they probably use the same code as the references you linked were the same to mine.

    One question though, are they tooltips that appear when you hover your mouse over an item in the inventory? I'm new to java/unity and I'm having trouble with mine. Would you be able to show me a sample of yours?

    Thanks.
     
  11. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    What I did for my tooltip is I check to see if my mouse is hovering over a button, if it is I check to see if that button has an item in it, if so I then create a gui box and add the information from the item in that spot.

    example:
    My gui button has gui content say "1" for the tooltip
    I then check gui.tooltip == "1" then I check if item is in that spot and if so then I just draw a gui box in the correct position with the items information.

    So to answer your question I dont use tooltip for the tooltip but I use tooltip to check if I need to make a tooltip.

    Sorry this is a little vague. My code atm is somewhat long and unfinished and would take me some time to butcher it down to something easilly for you to understad. I do plan on eventually polishing the code up and releasing it but atm I am too embaressed at its sloppyness. I havnt had much time to work at this project lately, started a new job and it slowed me right down.
     
  12. joel

    joel

    Joined:
    Jun 12, 2010
    Posts:
    117
    youre still working on this?
    it would be amazing to see the user being able to really assign 3d models to a player character.

    if you want i could help you out with some models?
     
  13. DjOscar527

    DjOscar527

    Joined:
    Nov 15, 2009
    Posts:
    800
    Looks awesome!
     
  14. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Nicely done!

    The sword doesn't fit anywhere now.

    Good that the backpacks can hold more stuff, both of them opening and working well.
     
  15. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,147
    This is really awesome, can you put it up for download. And one thing this really needs is a way to drop/destroy items. And improved visuals. The text was a little large. Other than that it looks really good!
     
  16. gogopie

    gogopie

    Joined:
    Jun 25, 2010
    Posts:
    11
    yeah if you could show how you made it that would be cool. I'm trying to make a similar thing for my game.
     
  17. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    Hi everyone, I wanted to say that I edited my main post and uploaded a rar file containing the source for this project.
    If you use this keep in mind I as a person am not a programmer, nor am I an artist or anything for that madder, I am just very ambitious and love to create games and share stuff with the lovely Unity Community.
    That being said my code may be VERY sloppy and uncommented, the models and everything are just place holders for you to replace.
    I decided to throw the source code out there because lots of people were asking about it, this may not be the best way to make an inventory but least now that it is out there hopefully I can get feedback on the code and everyone can work together to better it and perhaps make a solid inventory system.
    That being said there is one rule to using this code:
    TERMS OF USE:
    If you use this code and change it for the better, post the updated source back in the thread so we can better this project together.
    Its free to use in any commercial or non commercial game as long as you abide by the first rule.


    Anyway I hope you all like the source and all feedback is welcome always.
    Enjoy.
     
  18. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    hey, this is awesome thanks, i have some minor problems that i cant work out (new to coding)

    how would i go about moving and deleting some stuff that is in this picture?

    and keeping the bag button there all the time?

    any help would be greatly appreciated.

    [edit] forgot to ask also, how would i go about making the bag slots in the inventory?
     

    Attached Files:

  19. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    To move the gold over you would have to modify the first two values in the rects in this code:
    Code (csharp):
    1.  
    2.         GUI.Button(Rect(101,495,20,20),"G");
    3.         var gold = "";
    4.         if (inventorymoney[2] != null){
    5.             gold = gold + inventorymoney[2].itemstacksize;
    6.             GUI.Label(Rect(121,495,60,20),gold);
    7.         } else {
    8.             GUI.Label(Rect(121,495,60,20),"0");
    9.         }
    10.  
    11.  
    so something like:
    Code (csharp):
    1.  
    2.         GUI.Button(Rect(401,495,20,20),"G");
    3.         var gold = "";
    4.         if (inventorymoney[2] != null){
    5.             gold = gold + inventorymoney[2].itemstacksize;
    6.             GUI.Label(Rect(421,495,60,20),gold);
    7.         } else {
    8.             GUI.Label(Rect(421,495,60,20),"0");
    9.         }
    10.  
    By the looks of you picture you want to have one bag button that opens your inventory that has alot more spaces. You would first off increase the array of the inventory in the awake function, then you would call
    inventoryItemSlot(bankinventory,0,350,120);
    and place them where you want accordingly and how many, increasing the value of the first number each time so that it goes through the inventory array. Also you would prolly just get rid of the bags or just not put them in. You would also need an if statement around the inventoryItemSlot(bankinventory,0,350,120); changed by the button that opens your bag. Unless you are trying to pick one bag up and then have it so you can open your bag. You would prolly need to put the inventory down to size of 1 and then increase your bag size.

    Without me rewriteing the code for your specific needs this is the best explanation I can give, if you have any more questions or if anything I said sounded foreign just shoot some more specific questions, ill try my best to help.
     
  20. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    dude this is the best thing on the forum
    much people wants money for this

    but you dont even want a donation or somting
    YOU ARE THE BEST

    i and my team wil make a version with backgrounds etc if you want the file i wil post it :)

    but the button inventory is just the character screen??

    i want to know some things
    do i hold the items when i load a other scene??
    does the banker saves the stuff if i close the game??
     
  21. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    The art is seperate from agreement, you dont have to post, up to you.
    The inventory button can be changed, moved, removed what ever you desire.
    There is no saving or keeping items scene to scene, I didnt code that yet, but that is a very good idea, im going to have to try to implement this. Though if you need it soon you may want to code it yourself as I dont have much time lately.
     
  22. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    haha ok :)
    and i have to read the wholl script bevore i know where i put the no destruct part
    but maybe it a idea but the items that you put on the bank stored in a mysql lite?

    and i have a kind of equip script so you realy see it on the character ;)
    like you have a rpg the camera is in the back of the character
    so if you equip sotming you dont see anything

    do you need that?
     
  23. Cursed

    Cursed

    Joined:
    Apr 1, 2010
    Posts:
    7
    Omg thanks Unitrix for this i've been looking for a good inventory for ages :)

    Also Crasyboy42 Any chance of posting that Equipment script ;)
     
  24. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    it isnt realy a full script but it just equip it
    i wil post it tomorrow :)
     
  25. Caleb

    Caleb

    Joined:
    Aug 10, 2010
    Posts:
    4
    this is very cool! :) Thanks a lot for this!
     
  26. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    srry i am late i know XD

    but the character must have some empty's or you can attach them to the bone i dont care

    here is the script:
    var EmptyGameObject : Transform;
    var ArmorObject : Transform;


    function Update ()
    {

    }

    function OnTriggerEnter()
    {
    ArmorObject.parent = EmptyGameObject;
    ArmorObject.position=EmptyGameObject.position;
    ArmorObject.rotation=EmptyGameObject.rotation;
    }

    i use the ontrigger enter but you can change what ever you want :D

    for questions you must just ask ;)
     
  27. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    I had worked on this project while back and forgot to put some code up, this is for the bank to fix some bugs like tooltip not working and not allow potions to be used from there. I hope it works alright, like I said I wrote it a while ago and totally forgot about it.
    This all goes inside Inventory script.
    So first you make an array:
    Code (csharp):
    1.  
    2. var bankinventory : Array;
    3.  
    Then in the awake function:
    Code (csharp):
    1.  
    2. bankinventory = new Array(8);
    3.  
    Then the banking area replace the original with this:
    Code (csharp):
    1.  
    2.         bankinventoryItemSlot(bankinventory,0,350,120);
    3.         bankinventoryItemSlot(bankinventory,1,350,160);
    4.         bankinventoryItemSlot(bankinventory,2,350,200);
    5.         bankinventoryItemSlot(bankinventory,3,350,240);
    6.         bankinventoryItemSlot(bankinventory,4,391,120);
    7.         bankinventoryItemSlot(bankinventory,5,391,160);
    8.         bankinventoryItemSlot(bankinventory,6,391,200);
    9.         bankinventoryItemSlot(bankinventory,7,391,240);
    10.  
    then add this function to the script:
    Code (csharp):
    1.  
    2. function bankinventoryItemSlot(arraypassed,slot,xpos,ypos){
    3.     tooltipid = "ASDASD";
    4.     tooltipid = tooltipid + slot;
    5.         if (arraypassed[slot] != null  mouseitem == null){
    6.             if (GUI.Button(Rect(xpos,ypos,iconSize,iconSize),GUIContent ("", tooltipid))){
    7.                 if (Input.GetKeyUp(KeyCode.Mouse0)) {
    8.                     stackCheck();
    9.                     mouseitem = arraypassed[slot];
    10.                     arraypassed[slot] = null;
    11.                 }
    12.                 if (Input.GetKeyUp(KeyCode.Mouse1)){
    13.                     if (arraypassed[slot]!= null  arraypassed[slot].itemtype == "Stack"){
    14.                         if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)){
    15.                             //DO SOMETHING IF SHIFT LEFT CLICK HAPPENS
    16.                             //need to check if there is alredy a tooltip for amount picker if so then we can use any other items put if statements
    17.                             stackamount = 1;
    18.                             stackCheck();
    19.                             arraypassed[slot].showStack = true;
    20.                         }
    21.                     }
    22.                     if (arraypassed[slot]!= null  arraypassed[slot].itemtype == "Equip"){
    23.                         if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)){
    24.                             rightClickEquip(arraypassed,slot,true);
    25.                         } else {
    26.                             rightClickEquip(arraypassed,slot,false);
    27.                         }
    28.                     }
    29.                 }
    30.             }
    31.         } else {
    32.             if (GUI.Button(Rect(xpos,ypos,iconSize,iconSize), GUIContent ("", tooltipid))){
    33.                 if (Input.GetKeyUp(KeyCode.Mouse0)) {
    34.                     if (mouseitem != null){
    35.                         if (arraypassed[slot] != null){
    36.                             if (arraypassed[slot].itemtype == "Stack"){
    37.                                 arraypassed[slot].showStack = false;
    38.                             }
    39.                         }
    40.                         if (arraypassed[slot] != null  arraypassed[slot].itemtype == "Stack"  mouseitem.itemtype == "Stack"  arraypassed[slot].itemname == mouseitem.itemname ){// check if they are both the same type of stack else it
    41.                        
    42.                             if (arraypassed[slot].itemstacksize < arraypassed[slot].itemstacklimit){
    43.                                 arraypassed[slot].itemstacksize = arraypassed[slot].itemstacksize + mouseitem.itemstacksize;
    44.                                 if (arraypassed[slot].itemstacksize > arraypassed[slot].itemstacklimit){
    45.                                     mouseitem.itemstacksize = arraypassed[slot].itemstacksize - arraypassed[slot].itemstacklimit;
    46.                                     arraypassed[slot].itemstacksize = arraypassed[slot].itemstacklimit;
    47.                                 } else {
    48.                                 //need to chech here if together they are larger
    49.                                 Destroy (mouseitem.worldObject);
    50.                                 mouseitem = null;
    51.                                 }
    52.                             //do something
    53.                             } else {
    54.                                 //this take the variable from this slot and makes it temporary so we can put it in to mouse cursor
    55.                                 var tempaa = arraypassed[slot];
    56.                                 //what ever is in cursor will be put in the slot
    57.                                 arraypassed[slot] = mouseitem;
    58.                                 if (tempaa != null){ //IS THIS NECESARRY????????????
    59.                                     mouseitem = tempaa;
    60.                                 } else {
    61.                                     mouseitem = null;
    62.                                 }
    63.                             }
    64.                         } else {
    65.                             if (mouseitem.itemtype == "Bag"){
    66.                                 if (bagCheck() == false){
    67.                                     //this take the variable from this slot and makes it temporary so we can put it in to mouse cursor
    68.                                     var tempaaa = arraypassed[slot];
    69.                                     //what ever is in cursor will be put in the slot
    70.                                     arraypassed[slot] = mouseitem;
    71.                                     if (tempaaa != null){ //IS THIS NECESARRY????????????
    72.                                         mouseitem = tempaaa;
    73.                                     } else {
    74.                                         mouseitem = null;
    75.                                     }
    76.                                 }
    77.                             }   else {
    78.                                 //this take the variable from this slot and makes it temporary so we can put it in to mouse cursor
    79.                                 var tempa = arraypassed[slot];
    80.                                 //what ever is in cursor will be put in the slot
    81.                                 arraypassed[slot] = mouseitem;
    82.                                 if (tempa != null){ //IS THIS NECESARRY????????????
    83.                                     mouseitem = tempa;
    84.                                 } else {
    85.                                     mouseitem = null;
    86.                                 }
    87.                             }
    88.                         }
    89.                     }
    90.                 }
    91.                 //this is where we do the logic for right mouse click when we have an item in hand
    92.                 if (Input.GetKeyUp(KeyCode.Mouse1)) {
    93.                     if (arraypassed[slot] != null){
    94.                         if (arraypassed[slot].itemtype == "Equip"){
    95.                             if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)){
    96.                                 rightClickEquip(arraypassed,slot,true);
    97.                             } else {
    98.                                 rightClickEquip(arraypassed,slot,false);
    99.                             }
    100.                         }
    101.                     }
    102.                 }
    103.             }
    104.         }
    105.         if (arraypassed[slot] !=null){
    106.             GUI.DrawTexture (Rect(xpos,ypos,iconSize,iconSize),arraypassed[slot].itemtex);
    107.  
    108.             //I may be able to use a style that can do right to left alignment
    109.             if (arraypassed[slot].itemtype == "Stack"){
    110.                 stacksize = "";
    111.                 stacksize = stacksize + arraypassed[slot].itemstacksize;
    112.                 stackspace = 0;
    113.                 if (stacksize.length == 3){
    114.                     stackspace = 12;
    115.                 }
    116.                 if (stacksize.length == 2){
    117.                     stackspace = 19;
    118.                 }
    119.                 if (stacksize.length == 1){
    120.                     stackspace = 26;
    121.                 }
    122.                 GUI.Label(Rect(xpos+stackspace,ypos+19,iconSize,iconSize),stacksize);
    123.             }
    124.         }
    125.         if (mouseheld){
    126.             mouseOver = GUI.tooltip;
    127.         }
    128.         if (arraypassed[slot] != null){
    129.             if (arraypassed[slot].itemtype == "Stack"){
    130.                 if (arraypassed[slot].showStack){
    131.                     aaa = "";
    132.                     aaa = aaa + stackamount;
    133.                     //some kind of drag mouse increase decrease motion
    134.                     GUI.Box (Rect(900,700,100,50),GUIContent("","StackItemSelector"));
    135.                     GUI.DrawTexture (Rect(900,700,100,50),emptyTex);
    136.                     GUI.Label(Rect(940,700,100,100),aaa);
    137.                     if (GUI.Button(Rect(900,710,20,20),"<")){
    138.                         if (stackamount > 1){
    139.                             stackamount --;
    140.                         }
    141.                     }
    142.                     if (GUI.Button(Rect(980,710,20,20),">")){
    143.                         if (stackamount < arraypassed[slot].itemstacklimit){
    144.                             if (stackamount < arraypassed[slot].itemstacksize){
    145.                                 stackamount ++;
    146.                             }
    147.                         }
    148.                     }
    149.                     if (GUI.Button(Rect(900,730,50,20),"Okay")){
    150.                         arraypassed[slot].showStack = false;
    151.                         if (arraypassed[slot].itemstacksize == stackamount){
    152.                             arraypassed[slot].itemstacksize = stackamount;
    153.                             mouseitem = arraypassed[slot];
    154.                             arraypassed[slot] = null;
    155.                         } else {
    156.                             arraypassed[slot].itemstacksize = arraypassed[slot].itemstacksize - stackamount;
    157.                             newStack(slot,stackamount,arraypassed);
    158.                         } //when you click on another one you just take the mouse items stack size add it to the inventory and then make the mouse null
    159.                     }// you first check that its not too large otherwise you just add the item to it and then when you click again it would swap them
    160.                     if (GUI.Button(Rect(950,730,50,20),"Cancel")){
    161.                         arraypassed[slot].showStack = false;
    162.                     }
    163.                 }
    164.             }
    165.         }
    166.         itemToolTip(slot,xpos,ypos,arraypassed,tooltipid);
    167. }
    168.  

    I started working on this project again and if I update the code for the better I will post, lately I have just been butchering it down for a very simple game I am working on.
    But if I make any improvements ill post them.
     
  28. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Loved it, is it possible to implement a model? For example, I have an empty game object and id like to track if I have put an item into the primary slot. Once it tracks it, it will change the empty model to a new one that. That I can take care of (phew, atleast I have SOME brains).
     
  29. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    Yes it is possible.
    What I would probably do in this instance is check what number in the array the item I want to check for, so for example
    Code (csharp):
    1.  
    2.     equipedItemSlot(16,99,456,"Primary");
    3.  
    If I want to check for the primary as I can see in the code above the array slot is 16.
    So I would make an if statement that checks if the array at 16 is not null:
    Code (csharp):
    1.  
    2. if (EquipedItem[16] != null){
    3.  
    4. }
    5.  
    Then inside the if statement I would probably gather information about the item that is sitting there.
    You would probably need to add in the item script a variable for the name of the mesh you want displayed, then instantiate the prefab of the mesh that relates to the name and display it in the hand.
    Then if it is null take care of the prefab or something.
    This is just something similar to what I would attempt to do, I havnt tried this yet because im still ironing out some bugs and what not and working on other projects.
    Anyway good luck with this and maybe if you get something working you can post it back here to help others out :)
     
  30. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28
    i am working on the equip stuf but i have a problem
    if the item has been added to the inventory it is gone

    but how do i get it back so it can equip it??
    or where does the item stands??

    i wil post the code when done :D
     
  31. Epic

    Epic

    Joined:
    May 30, 2010
    Posts:
    76
    Adding 3d models to a character wouldbe good
     
  32. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Thanks so much. That idea is actually quite clever :wink:

    Ill see if I can get it working. Sorry im repling 2 days late, I havent had internet for the last 2 days.
     
  33. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    It works, sorta.

    The things I had trouble with were setting it to the game object of the weapon. That would require static variables but those are always null :( , then I tried

    if(EquipedItem[16] == null), and the only quirk was that I didn't know where to put it when not equiped...

    the full code (thanks to 2 aurthors of this post)
    Code (csharp):
    1.  
    2. if (EquipedItem[16] != null)
    3.     {
    4.     //A.K.A. : ArmorObject = EmptyGameObject;
    5.     ArmorObject.parent = EmptyGameObject;
    6.     ArmorObject.position=EmptyGameObject.position;
    7.     ArmorObject.rotation=EmptyGameObject.rotation;
    8.     }
    9.  
    thanks you all for all your help so far :D
     
  34. crasyboy42

    crasyboy42

    Joined:
    Jun 29, 2010
    Posts:
    28

    but if you take a item it wil be disappear/delete
    how did you get it back??
     
  35. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    If you are talking about just adding the visual effect of the armor to the character, create a prefab that is just that, the model with its textures.
    Like he has here:
    Code (csharp):
    1.  
    2. if (EquipedItem[16] != null)
    3.    {
    4.    //A.K.A. : ArmorObject = EmptyGameObject;
    5.    ArmorObject.parent = EmptyGameObject;
    6.    ArmorObject.position=EmptyGameObject.position;
    7.    ArmorObject.rotation=EmptyGameObject.rotation;
    8.    }
    9.  
    Do something like instantiate this prefab based on the item that is in the inventory slot, so say you have sword 1 in there, check if its sword 1 and if so then create prefab with the model and textures for sword 1. Then do what he did with parenting and adding position/rotation.
    If you dequip it just destroy the prefab in an else statement.

    Anyway something like that.
     
  36. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Yeah, that was one of my ideas. Instantiate and destroy. Still, to tell wich prefab to instantiate it would be most efficient to do in the itemscript? Whould it work to do that in the item class section.
     
  37. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Ive been at it for 2 days(im new at this so kinda lo patients), conclusion - I must use a static variable in the item script, that can change...how? I just tried codding items. I thought I hit the jackpot. Then I rialized I couldn't change the code cause it was static... :x Any ideas :?:
     
  38. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    In ItemScript.js add this:
    Code (csharp):
    1.  
    2. var itemmesh : String = "Name of mesh";
    3.  
    add this to
    OnMouseDown Function in same script:
    Code (csharp):
    1.  
    2. item.itemmesh = itemmesh;
    3.  
    In Inventory.js add this to class InventoryItem
    Code (csharp):
    1.  
    2. var itemmesh : String;
    3.  
    Then when you do your check for example:
    Code (csharp):
    1.  
    2. if (EquipedItem[slot] != null){
    3.  if (EquipedItem[slot].itemmesh == "Name of Mesh"
    4.                         var ArmorObject = Instantiate(nameofprefabarmor, transform.position, Quaternion.identity);
    5.                         ArmorObject.parent = EmptyGameObject;
    6.    ArmorObject.position=EmptyGameObject.position;
    7.    ArmorObject.rotation=EmptyGameObject.rotation
    8.  
    Then create a prefab and link it in the inspector to nameofprefabarmor. Of corse you need to add this:
    var nameofprefabarmor : Transform; at the top of the inventory.js.

    This is just a rough idea of what you need to do. I will eventually be working on this myself but I am caught up with something else currently.
     
  39. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Hm, I finally installed that into my scripts. Ill toy around with it until, I find an answer. Ill do my best to help you finish this package. :wink: (Optamistic)
     
  40. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    I forgot to mention, the prefab that you instantiate is just a mesh/model for viewing the item. It should also contain a script that has var script : Inventory;
    Then link the player with the inventory.js into it, then what you can do is make a check like, if ( script.EquipedItem[script.slot] == null){
    //destroy gameobject
    }

    This will basically have the mesh check if the equiped slot that it goes to is not null and if it is destroy it so its no longer displayed.
    You can always just use script.EquipedItem and the number you put in here [ ] relates to the equiped item number slot. So say for example if you equiped item slot 5 is for hands, then put 5 in there.

    Anyway I hope this helps.
    Im not 100 % sure this is the best way to do it but its one way to do it.
    Maybe if someone knows a better way I would love to hear it and discuss and work upon it.
     
  41. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    (Removed)
     
  42. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    I'm actually quite interested in building upon the file that unitrix posted but the link for download is corrupt or just simply doesn't work. If someone could post it again that'd be much appreciated.

    Cheers
     
  43. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Here!
     

    Attached Files:

  44. SilverFoxMedia

    SilverFoxMedia

    Joined:
    Nov 7, 2009
    Posts:
    153
    Thanks, I'll be sure to share any additions I make to it here in the future.
     
  45. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Ok, I got it :D .

    I have it in the Update function. But it has a t/f statment to controll it. So, if update needed = true, run the statement and set update needed to false. Then when you desrtory it, set it back to true! :D :D
     
  46. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Hm. I am stupped, again. I am at deleting the object now. I tried:

    Code (csharp):
    1.  
    2. if(UpdateNeeded == false)
    3.     {
    4.         if(EquipedItem[16] == null)
    5.         {
    6.                 Destroy(ArmorObject);
    7.                 UpdateNeeded = true;
    8.         }
    9.     }
    10.  
    By the way, here is the equipping code:
    Code (csharp):
    1.  
    2. if(UpdateNeeded)
    3.     {
    4.         if (EquipedItem[16] != null)
    5.         {
    6.             if (EquipedItem[16].itemmesh == "Staff")
    7.             {
    8.                 var ArmorObject = Instantiate(nameofprefabarmor, transform.position, Quaternion.identity);
    9.                 ArmorObject.parent = EmptyGameObject;
    10.                 ArmorObject.position=EmptyGameObject.position;
    11.                 ArmorObject.rotation=EmptyGameObject.rotation;
    12.                 UpdateNeeded = false;
    13.             }
    14.         }
    15.     }
    16.  
    Yes, nothing much has changed about it. :oops:
     
  47. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    What I can see being one problem is that the ArmorObject is instantiated in a different scope, that meaning that if you do var ArmorObject within a if statement it wont carry over to another if statement. Also I am not sure if you can reference the prefab that you instatiated from the inventory. This is why I said try to create a script and add it to the prefab then add this to the script:
    Code (csharp):
    1.  
    2. var script : Inventory;
    3.  
    4. function Update(){
    5. if(script.UpdateNeeded == false)
    6.    {
    7.       if(script.EquipedItem[16] == null)
    8.       {
    9.             Destroy (gameObject);
    10.             script.UpdateNeeded = true;
    11.       }
    12.    }
    13. }
    14.  
    Make sure that var script : Inventory is the name of the inventory.js and then link the object or player that has the inventory script to the prefab that has the var script to it.

    Hope this helps.
     
  48. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    Erm... Got lost, my player model has the inventory script... Should I put it somewhere else?
     
  49. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    The Player Object having the inventory script is fine, if you make the mesh prefab and add that script to it I just had up above then in the mesh prefab you have to drag and drop the player onto the script var in the inspector.
     
  50. Zu01

    Zu01

    Joined:
    Aug 17, 2010
    Posts:
    95
    So for Inventory: Missing (Inventory). I should choose My character. The problem is that it destroyes the character if I do that...
     
unityunity