Game Development Reference
In-Depth Information
{
invItems[i] = EmptyObject;
items.Add(new KeyValuePair<int,
GameObject>(i, invItems[i]));
itemCount.Add(new KeyValuePair<int, int>(i,
0));
if(i < QuickItems.Length)
QuickItems[i] = invItems[i];
}
}
The first line within the new function sets the invItems array size to the Invent-
orySize variable. Next, we have a for loop that will initialize the inventory. First, it
sets each invItem value to our EmptyObject GameObject variable, which is our
placeholder until we start adding items to the inventory.
Next, we add a new KeyValuePair variable to each slot within the items list. The
key of the new KeyValuePair variable will be our iterator variable from the for
loop. The value of the new KeyValuePair variable will be the GameObject within
the invItems array that currently holds the spot that our iterator is valued at. This
is so that the invItems array and items list are ordered in the same way.
After this, we add a new KeyValuePair variable to our itemCount list. The key
is going to be set to our iterator variable as well and the value will be set to 0. This
will ensure that every item in our inventory will have no value assigned to it, until we
start adding items.
The last two lines in our for loop will create our default quick items. We use an if
statement to check whether the value of our iterator is still less than the length of our
QuickItems array. If it is, we set each QuickItems in the array to what is in our
invItems array, which is our EmptyObject GameObject. To call this function, we'll
put it within an Awake function, as follows:
void Awake()
{
Search WWH ::




Custom Search