U
U
Uncle Bogdan2021-07-12 15:40:30
Unity
Uncle Bogdan, 2021-07-12 15:40:30

How to normally implement the selection / display of inventory items?

The inventory works, but there is a problem with the items.

The inventory is implemented using a scriptable object

. The item has its maximum quantity:

Planks: the maximum number is 32

Ax: the maximum quantity is 1

When picking up the quantity of the item, its quantity is calculated: There are 5 planks, then check if the player has no planks, then .Add if is, then add the number of boards lying to this element of the Items sheet.

Can this be improved somehow?

Now:

public static void AddItem(Item _item, int Amount)
{
if(Items.Find(item => item == _item) != null)
{
var item = Items.Find(item => item == _item);
item.Amount += Amount;
}
else // Если нету такого
{
_item.Amount += Amount;
Items.Add(_item);
}

}


Main problem:

I have already said about the maximum number, and so. Now there is a restriction variable, but it is not used in any way. The list of items is filled in like this:

private void RenderItems()
    {
        foreach (Transform child in _contaner)
        {
            Destroy(child.gameObject);
        }

        Inventory.Items.ForEach(item =>
        {
            var cell = Instantiate(_inventotyCell, _contaner);
            cell.Render(item);
        });
    }


And as you can see now there are no restrictions, but it is necessary so that if, for example, the maximum number is 10, and there are 16 items, then enter 10 in the first cell, and 6 in the second. And for example, if the maximum speed is 10, but there are 84, then there will be 8 by 10 and one for 4. I think the meaning is clear. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2021-07-12
@twobomb

You have _item that is passed as an argument, that one is added to the array. And if you need 8 of them, then you need to take them somewhere. As an option, pass the item id, and already create in the method. like this

public static void AddItem(int itemId, int Amount){
  
  int inventoryCellCount = 20;//Количество ячеек инвентаря
  int maxCount = GetMaxCountById(itemId);//Получить максимальное кол-во стека для этого айтема по ид
  
  while(Amount > 0){
    var item = Items.Find(item => item.id == itemId);
    if(item == null || item.Amount == maxCount){
      if(Items.Count >= inventoryCellCount)//Если ячейки инвентаря забиты
        break;
      item = CreateItemById(itemId);//создает итем по ид
      Items.Add(item);			
    }	
    if(item.Amount + Amount > maxCount){		
      Amount = Amount - (maxCount - item.Amount);
      item.Amount = maxCount;
    }
    else{
      item.Amount += Amount;
      Amount = 0;
    }
  }
}

PS Well, there is a drawback here, let's say we raise 80 items. and we have places only for 20 and then the inventory is clogged. Here you need to think about the feedback, well, like it’s trivial to return the residual Amount and write it to the object on the ground, if it is equal to zero, then delete the object from the ground, if it is greater than zero, overwrite the quantity. Well, or a radical method to remove the item from the ground after the selection is complete, and if everything doesn’t fit, then it will simply disappear, it’s up to you...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question