#when an item becomes ten I want it to

1 messages · Page 1 of 1 (latest)

dry flax
#

I'm creating a thread so that we don't bother other users

tardy ocean
#

ok

dry flax
#

We'll have to modify some stuff so that it works as intended

#

1st, in InventorySlot, the method AddAmount should be responsible of the clamping, but should return the excess amount too so that you know what to give to your new slot.

public int AddAmount(int value)
{
  amount += value;
  int excess = amount % 10;
  amount = Math.Clamp(amount, 0, 10);
  return excess;
}
#

Now, you could improve it by replacing 10 by a variable so that it's more robust and you have better control of the max value of each slot

#

2nd, in your InventoryObject, the method AddItem can stay the same, but we'll change the part that handles the item amount

if (Container[i].item == _item && _item.type == ItemType.Default)
            {
                int excess = Container[i].AddAmount(_amount);
                if (excess > 0)
                {
                    Debug.Log("enough");
                    Container.Add(new InventorySlot(_item, excess));
                }
                hasItem = true;
                break;
            }
#

And this should do the trick 🙂

tardy ocean
#

I'ma test it out

dry flax
#

Ah yes, the remnant, my bad haha

#

Let me fix this real quick

#
public int AddAmount(int value)
{
  amount += value;
  int excess = 0
  if(amount > 10)
  {  
    excess = amount % 10;
    amount = Math.Clamp(amount, 0, 10);
  }
  return excess;
}
#

I forgot the remnant of A if A < 10 will not be 0...

tardy ocean
#

wait holdup

dry flax
#

But this won't work if you add 20 for example

#

You'd need to make a recursion or a loop

#

The actual excess is wrong... it should be it. Sry, i'm focused on multiple things and making some mistakes

public int AddAmount(int value)
{
  amount += value;
  int excess = 0;
  if(amount > 10)
  {  
    excess = amount - 10;
    amount = Math.Clamp(amount, 0, 10);
  }
  return excess;
}
tardy ocean
#

there's a problem, as it hits 10, it does creates a new item but it doesn't increase its amount so instead of changing the amount, it just creates an another one instead

#

so basically it doesn't loop

dry flax
dry flax
#

Ah yes

#

That's the problem I identified with your code

#

The thing is, it alters the 1st found slot which is always the same one

#

You need to modify the code to look for the 1st available/not full slot 1st, and create a new one only if : the slot is not enough OR if there's no available slot

tardy ocean
#

I'ma continue that tomorrow since I have to wake up early in the morning

#

tyvm for the help btw