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 /
  • Help Room /
avatar image
0
Question by VagabondPrism · Mar 08, 2017 at 08:17 AM · inventory systemitem pickupstacking

Items not adding correct values to inventory when multiple colliders hit at once. (2D Game)

I've been working on this inventory system using bool functions so that I can sort everything out fairly logically. All seems to be working decent so far except when I kind of overload it. I've got a script that pulls items within a certain radius to the character and when it hits the character's BoxCollider2D it runs this:

     private void OnTriggerEnter2D(Collider2D otherCol)
     {
         //Debug.Log("collided");
         if(otherCol.tag == "Item" && inv.emptyinvSlots > 0)
         {
             Item collidedItem = otherCol.gameObject.transform.GetComponent<ItemData>().thisItem;
 
             if(inv.AddItem(collidedItem, otherCol))
             {
                 //TODO: add in pickup sound
                 Destroy(otherCol.gameObject);
             }
             else
             {
                 otherCol.GetComponent<AttractorBehavior>().attracting = false;
             }
   
           }
     }


My Inventory script has the functions below. This issue becomes when I collect a bunch of items the values aren't adding properly. It seems like some get ignored or are running at the same time and get written over or something. I have 10 items of the same type (with a stack size of 5 & a stackCurrentCount of 1) in front of my character and when I run into the group and they all start pulling toward the character they add correctly and I end up with ten. However if I try to make some of the item have a higher stackCurrentCount the values get lost.

 public bool AddItem(Item item, Collider2D itemCollider)
 {    int startingCount = item.stackCurrentCount;
     Debug.Log("Starting AddItem - size count: " + startingCount + "    Item UniqueID: " + item.uniqueID);
     if(CombineStack(item, itemCollider))
     {    //If item added to stack and has a remainder, run again.
         Debug.Log("After Combinestack runs once: Item stack size is:" + item.stackCurrentCount);
         if(item.stackCurrentCount > 0 && item.stackCurrentCount < startingCount)
         {
             Debug.Log("Attempting Second CombineStack:  " + item.stackCurrentCount + "Current Count");
             if(CombineStack(item,itemCollider))  //Try to combine with a new stack.
             {
                 
             }
             else if(PlaceEmpty(item, itemCollider))  // If no stacks to combine with, try to add to an empty slot.
             {
                 Debug.Log("Placed in Empty after second combine stack failed.");
                 return true;
             }
             else
                 Debug.Log("SecondCombineStackattemptfailed");
             {return false;}
         }
 
         return true;
     }
     else if(PlaceEmpty(item, itemCollider))
     {
         return true;
     }
     else{ //If Combine unsuccessful return false.
         return false;}
 }
 
 
 public bool PlaceEmpty(Item item, Collider2D itemCollider)
 {    //Search invArray and add an item to the first empty slot.
     for(int x = 0; x < invArray.Length; x++)
     {
         if(invArray[x].uniqueID == "UNSET" && emptyinvSlots > 0)
         {
             Debug.Log("Placing in empty slot");
             invArray[x] = item;
             emptyinvSlots--;
             UpdateSlotIcon(item,x);
             return true;
         }
 
 
     }
     //If place unsuccessful return false.
     return false;
 
 }
 //Called from in world pickup.
 public bool CombineStack(Item item, Collider2D itemCollider)
 {    // Search invArray for existing stack. Add if it exists combine
     for(int x = 0; x < invArray.Length; x++)
     {
         
         if(invArray[x].itemName == item.itemName && (invArray[x].stackCurrentCount + item.stackCurrentCount) <= invArray[x].maxStackSize)
         {  // If item matches existing item & stack count plus new items is less than max stack size, add to current stack.
             Debug.Log("Stack: " + invArray[x].stackCurrentCount + " to be added: " + item.stackCurrentCount);
             invArray[x].stackCurrentCount += item.stackCurrentCount;
             Debug.Log("After add: " + invArray[x].stackCurrentCount + "           Loop Num:" + x);
             UpdateSlotIcon(invArray[x], x);
             return true;
         }
         else if(invArray[x].itemName == item.itemName && (invArray[x].stackCurrentCount + item.stackCurrentCount) > invArray[x].maxStackSize && item.stackCurrentCount > 1)
         {    // If item matches existing item & stack count plus new items is more than max stack size add what's possible. rerun combine stack.
             int differenceFromMax = invArray[x].maxStackSize - invArray[x].stackCurrentCount;
             invArray[x].stackCurrentCount += differenceFromMax;
             UpdateSlotIcon(invArray[x], x);
             item.stackCurrentCount -= differenceFromMax;
 
             return true;
 
         }
         else{
             Debug.Log("CombineStackFailed");
             return false;
         }
     }
 
     return false;
 }
 

Tests:

  • I've tried setting a single item with a higher currentStackCount and collecting it by itself. I get the correct value.

  • Setting two items with a size of 2 on one and 3 on the other: Correctly adds to five.

  • Setting two items with a size 3 on both: I get 5 on one stack and a new stack of 1.

  • Setting two items with a size of 3 and a third item with size 1: I get a stack of 5 and two single stacks. When picking up in the order of 3,3,1.

  • Setting two items with a size of 3
    and a third item with size 1: Picking up in order of 3, 1, 3. It adds correctly until I get to the last item. Then it adds one to my stack and quits. Debug Log gets to the point of "Attempting Second Combine Stack" The gameobject stops being drawn toward the player. Then when I walk away and walk back to it, it tries to collect but doesn't add to a new slot & Debug Log shows CombineStack started it's initial run. I don't understand how combine stack

I know the issue has to be with the logic for the second combine stack but I tried to rerun the CombineStack function from inside itself and froze up Unity. Thanks in advance for any advice!!

Comment
Add comment · Show 1
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
avatar image VagabondPrism · Mar 08, 2017 at 08:53 AM 0
Share

I seem to have gotten it working so far. I've changed a few of the inner functions from bool to void and resorted the logic. The issue I have now is that it's really choppy when I collect a bunch of items at once. Is there a way to reduce that?

The change code is below:

 public bool AddItem(Item item, Collider2D itemCollider)
 {    int startingCount = item.stackCurrentCount;
     Debug.Log("Attempting Combine Stack");
     CombineStack(item, itemCollider);
     if(item.stackCurrentCount != 0)
     {
         PlaceEmpty(item, itemCollider);
         return true;
     }
         
     if(item.stackCurrentCount == 0)
     {
         return true;
     }
 
     return false;
 }
 
 
 public void PlaceEmpty(Item item, Collider2D itemCollider)
 {    //Search invArray and add an item to the first empty slot.
     for(int x = 0; x < invArray.Length; x++)
     {
         if(invArray[x].uniqueID == "UNSET" && emptyinvSlots > 0)
         {
             Debug.Log("Placing in empty slot");
             invArray[x] = item;
             emptyinvSlots--;
             UpdateSlotIcon(item,x);
             break;
         }
     }
 }
 
 
 //Called from in world pickup.
 public void CombineStack(Item item, Collider2D itemCollider)
 {    // Search invArray for existing stack. Add if it exists combine
     for(int x = 0; x < invArray.Length; x++)
     {
         Debug.Log("CombineStack Iteration: " + x);
         if(invArray[x].itemName == item.itemName && (invArray[x].stackCurrentCount + item.stackCurrentCount) <= invArray[x].maxStackSize)
         {  // If item matches existing item & stack count plus new items is less than max stack size, add to current stack.
             Debug.Log("Stack: " + invArray[x].stackCurrentCount + " to be added: " + item.stackCurrentCount);
             invArray[x].stackCurrentCount += item.stackCurrentCount;
             item.stackCurrentCount -= item.stackCurrentCount;
             Debug.Log("After add: " + invArray[x].stackCurrentCount + "           Loop Num:" + x);
             UpdateSlotIcon(invArray[x], x);
             break;
 
         }
         if(invArray[x].itemName == item.itemName && (invArray[x].stackCurrentCount + item.stackCurrentCount) > invArray[x].maxStackSize && item.stackCurrentCount > 1)
         {    // If item matches existing item & stack count plus new items is more than max stack size add what's possible. rerun combine stack.
             int differenceFrom$$anonymous$$ax = invArray[x].maxStackSize - invArray[x].stackCurrentCount;
             invArray[x].stackCurrentCount += differenceFrom$$anonymous$$ax;
             UpdateSlotIcon(invArray[x], x);
             item.stackCurrentCount -= differenceFrom$$anonymous$$ax;
             Debug.Log("Added what could be, breaking Combine Stack loop");
 
         }
     }
 }

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

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Inventory system is in a big mess now, help me, please 0 Answers

Item is missing in inventory slot after i pick up and delete the item. C# 0 Answers

the unable stack item(equip) change to stack item(consume) and amount increase. 0 Answers

Adding a prefab to the Item class instance 0 Answers

inventory for android 0 Answers


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