Now, we'll add the second most important aspect of inventories, removing items! This is handy for those times when a player uses their health potion, sells an item, shoots a rocket, or drops their coins!
Just like we did when we added items, let's take a moment to think about how we want to remove items from the inventory. Again, from the player's point of view, how is this done? Well when they sell the item, they are selecting the item personally. If the player shoots their gun or bow, their ammo is dispensed immediately. When the player meets their untimely death, their items may be dropped on the ground or left on their corpse to be looted by their assailant. During the selling item phase and shooting gun action, they pick what item they want to get rid of from their inventory. We will follow a similar process when removing items from the inventory.
Add the following function to the script, just below the AddToInventory
function:
void RemoveFromInventory(int HowMany, GameObject Item) { for(int i = 0; i < items.Capacity; i++) { if(invItems[i].name != "Empty") { if(invItems[i].name == Item.name) { int val = itemCount[i].Value - HowMany; itemCount[i] = new KeyValuePair<int, int>(itemCount[i].Key, val); if(itemCount[i].Value <= 0) { invItems[i] = EmptyObject; items[i] = new KeyValuePair<int, GameObject>(i, EmptyObject); itemCount[i] = new KeyValuePair<int, int>(itemCount[i].Key, 0); } break; } } } }
Just as when we added items to the inventory, we pass an int
variable and a GameObject to this function. The int
variable is the number of the items we want to remove, and the GameObject is the actual item that we want to remove. Now we iterate through the items array, which is the array holding our inventory of items.
First we check whether the current invItem
variable's name isn't "Empty"
. If it isn't, then we move on to see whether the name of the current invItem
variable is equal to the name of the item we want to remove from the inventory. Similar to how we added items to the inventory, we'll need to create a new int
variable, which will hold the value of the item that we'll decrease. This time, val
will be equal to the current itemCount
value subtracted from the HowMany
integer that was passed into the function. We then assign the current itemCount
variable a new KeyValuePair
variable, using the same key as key
and using val
as the value.
Now, we do something a little different from what we did while adding items. We check whether the current itemCount
value is less than or equal to zero; if it is, we have to do a few things. When an item has an amount of zero or less we have to remove it from the inventory.
Our first step to remove the item from the inventory will be to set the current invItem
value to our EmptyObject
variable, the placeholder. Next, we set the current item's KeyValuePair
variable to new KeyValuePair
, the new KeyValuePair
variable will have the iterator as the key and the EmptyObject
GameObject as its value. Finally, we set the current itemCount
KeyValuePair
to a new KeyValuePair
as well. Its key will stay the same, but we set the value to 0
.
After this is all done, we stop the loop with break
. Remember when we checked whether the current invItems
name was equal to "Empty"
? If it does happen to be "Empty"
, we use break again to stop the loop. We can't remove the item from the inventory, since it is in the inventory to begin with!
With that, you now have the ability to remove items from the inventory. When the player sells an item, shoots an arrow, use a health potion, or anything similar to these situations, you can remove that specific item from the inventory. This may seem like a daunting task, but there is a way around this. You can also use the InitializeInventory
function that we first created to reset the inventory, since that is essentially what that function does.
3.14.252.56