- Home /
Question about an inventory system idea
So I have a basic.idea of how I can do an inventory but I dont know a thing really important. When I enter in the object's collider it is destroyed and added in the inventory by just saying inventory.Add but I dont know how in the inventory I can add this item graphically to the first slot. I know.I can set.the item icon would be the same as the item's but how can I set the first slot? And if the first.slot isn't empty the second?
run a check on your array you are using. if slot one is not == null assign it there else check next slot until it finds one that is equal to null. then have a function that takes a int as that will be the location the item is in the array and use it to assign a graphic in the bag with
So I have a list of items that are 30 slots in the inventory. When I pick up an item I add.it to the inventory and give it and ID likee 1 and in the slot script i say: if(itemThere == null) itemThere.= 1. slotEmpy = false; Could be this a goodway? And in the second slot i say if slot 1.slotEmpty = false itemThere = 2. Is it.understable?
Answer by Fire_Cube · Jul 03, 2016 at 07:37 PM
you loop in every slot possible, and check if they are empty. If they are, you place the item there and stop the loop. If none are, just drop the item. Just do a mono behaviour on every slot that contain the item and make it render.
Can you please give me an example in c#? I still dont understand.
Answer by pipe01 · Jul 05, 2016 at 08:49 PM
This is an example to what Fire_Cube said:
bool AddItem(int item)
{
foreach (var slot in inventory.slots)
{
if (slot != -1)
{
slot = item;
return true;
}
}
return false;
}
This function will return true if the item was successfully asigned to a slot, or false if there isn't enough space, in which case you may drop the item or whatever.
Also, this is assuming that your items are stored as integers, and that an empty slot is represented as -1.