Answer the question
In order to leave comments, you need to log in
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);
}
}
private void RenderItems()
{
foreach (Transform child in _contaner)
{
Destroy(child.gameObject);
}
Inventory.Items.ForEach(item =>
{
var cell = Instantiate(_inventotyCell, _contaner);
cell.Render(item);
});
}
Answer the question
In order to leave comments, you need to log in
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;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question