Game Development Reference
In-Depth Information
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 cur-
rent 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 in-
vItem 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 .
Search WWH ::




Custom Search