Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sporkflips · Jun 22, 2016 at 05:07 AM · c#inventoryinventory systemfor loopfor

Issues with Inventory script

I'm working on an inventory system and I was following this tutorial series and I've got most of what's in that series (parts 1-8 at least) working right but I'm having issues with two things.. one is the dragging and dropping to move items.

for some reason it only works if you click outside of the Rect containing the item you want to drag and then drag onto the Rect with the mouse button already held down (doesn't work if you have the mouse over the Rect and then click and drag away which is what I'm going for and what's shown in the tutorial series - in part 8).. the code that has that part in it is this

 void DrawInventory()
 {
     Event e = Event.current;
     int i = 0;

     for (int y = 0; y < slotsY; y++)
     {
         for (int x = 0; x < slotsX; x++)
         {
             Rect slotRect = new Rect(invLocX + (x*55), invLocY + (y*55), 50, 50);
             GUI.Button(slotRect, "", skin.GetStyle("Inventory Slot"));
             slots[i] = playerInventory[i];
             if(slots[i].itemName != null)
             {
                 GUI.DrawTexture(slotRect, slots[i].itemIcon);
                 GUI.Label(slotRect, playerInventory[i].quantity.ToString(), skin.GetStyle("Quantity Label"));
                 if(slotRect.Contains(e.mousePosition))
                 {
                     if (slots[i].questItem) questItem = "This is a quest item.";
                     else questItem = "";
 
                     CreateTooltip(slots[i]);
                     if(!draggingItem) showTooltip = true;

                     if (e.button == 0 && e.type == EventType.MouseDrag && !draggingItem)
                     {
                             draggingItem = true;
                             prevIndex = i;
                             draggedItem = slots[i];
                             playerInventory[i] = new Item();
                     }
                     if (e.type == EventType.MouseUp && draggingItem)
                     {
                         playerInventory[prevIndex] = playerInventory[i];
                         playerInventory[i] = draggedItem;
                         draggingItem = false;
                         draggedItem = null;
                     }
                 }
             }
             else
             {
                 if(slotRect.Contains(e.mousePosition))
                 {
                     if (e.type == EventType.MouseUp && draggingItem)
                     {
                         playerInventory[i] = draggedItem;
                         draggingItem = false;
                         draggedItem = null;
                     }
                 }
             }
             if (tooltip == "")
                 showTooltip = false;

             i++;
         }
     }
 }

Then the other thing I'm having trouble with is some stuff I added to the AddItem function to try and get it so that items can stack. it works to add single items and stacks of items to the inventory. but if I try to stack onto a slot that has an item in it but isn't at maxStack or if I try to add items in a quantity that's larger than those items can stack to be it gets all messed up... this is what I have for the AddItem function. I think I must be doing something wrong with assigning the quantities... or maybe I need another variable to hold items while they're getting stacked properly or something so that it doesn't overwrite the assignments in other parts of the function? I mean I thought that if I assign thingA the value of thingB in one part of the script, it would only change with thingB if the part of the script that has that assignment in it gets called again. so I probably am also not understanding how unity moves through all these for loops that I have...

I would appreciate any help that anyone may have to offer about either of these issues..

 void AddItem(int id, int quant)
 {
     for (int j = 0; j < database.items.Count; j++)
     {
         if (database.items[j].itemID == id)
         {
             database.items[j].quantity = quant; //ALWAYS set the relevant quant when referencing database items
             print("1:" + quant+ "a:" + database.items[j].maxStack);

             if (database.items[j].maxStack == 1)
             {
                 for (int i = 0; i < playerInventory.Count; i++)
                 {
                     if (playerInventory[i].itemName == null)
                     {
                         playerInventory[i] = database.items[j];
                         quant--;
                         if (quant == 0)
                         {
                             quantLeftover = 0;
                             return;
                         }
                     }
                     else if (i == (playerInventory.Count - 1) && playerInventory[i].itemName != null)
                     {
                         inventoryIsFull = true;
                         quantLeftover = quant;
                         return;
                     }
                 }
             }
             else if (database.items[j].maxStack > 1)
             {
                 for (int i = 0; i < playerInventory.Count; i++)
                 {
                     if (playerInventory[i].itemID == id && playerInventory[i].quantity < database.items[j].maxStack)
                     {
                         print("2:" + quant+ "b:" + playerInventory[i].quantity);
                         for (int k = playerInventory[i].quantity; k < playerInventory[i].maxStack; k++)
                         {
                             playerInventory[i].quantity++;
                             quant--;
                             print("3:" + quant+ "c:" + playerInventory[i].quantity);
                             if (quant == 0) return;                                                     
                         }
                     }
                     else if (playerInventory[i].itemName == null)
                     {
                         print("4:" + quant+ "d:" + playerInventory[i].quantity);
                         if (quant <= database.items[j].maxStack)
                         {
                             playerInventory[i] = database.items[j];
                             playerInventory[i].quantity = quant;
                             quant = 0;
                             print("5:" + quant + "e:" + playerInventory[i].quantity);
                             quantLeftover = quant;
                             return;
                         }
                         else if (quant > database.items[j].maxStack)
                         {
                             print("6:" + quant + "f:" + playerInventory[i].quantity);
                             database.items[j].quantity = database.items[j].maxStack;
                             playerInventory[i] = database.items[j];                               
                             quant -= database.items[j].maxStack;
                             print("6:" + quant + "f:" + playerInventory[i].quantity);
                         }
                     }
                     else if (playerInventory[i].itemID == id && playerInventory[i].quantity == playerInventory[i].maxStack && quant > 0)
                     {
                         inventoryIsFull = true;
                         quantLeftover = quant;
                         return;
                     }
                     print("7:"+quant+ "g:" + playerInventory[i].quantity);
                     if (quant == 0)
                     {
                         quantLeftover = 0;
                         return;
                     }
                 }
             }
         }
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Inventory AddItem help 1 Answer

How do I make my item only open if there is room in my Inventory? 3 Answers

chest items get added to inventory 2 Answers

Clone a component, add it to a list and destroy the GameObject 1 Answer

Searching your inventory for an item,How do i search for an item in my inventory? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges