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!!
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");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612112713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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