Game Development Reference
In-Depth Information
Removing items
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!
Let's figure this out
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 shoot-
ing 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.
Creating the removing function
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;
Search WWH ::




Custom Search