Game Development Reference
In-Depth Information
else
{
int val = itemCount[i].Value + HowMany;
invItems[i] = NewItem;
items.Add(new KeyValuePair<int,
GameObject>(i, NewItem));
itemCount.Add(new KeyValuePair<int,
int>(i, val));
break;
}
}
}
The first thing you'll notice is that our function takes in two variables, an int and a
GameObject variables. The int variable is the number of new items that we want
to add to the inventory. The GameObject is the new item that we want to add to the
inventory. Now, let's go over how exactly we are going to add items to the inventory.
We use a for loop to iterate through each item of our invItems array, since this
is the array that is holding our inventory items. First, we check whether the name
of current invItem is not empty. The only object that we'll be using with the name
"Empty" is actually our EmptyObject variable, which is our placeholder object.
So if the current invItem isn't empty, we move on to check whether its name is
equal to that of the new item's name. If it is, we create a new int variable named
val . The val integer is assigned the total of the current invItem variable's value
and the value of the new item we want to add. After we do this, we set the current
itemCount value to val by assigning it a new KeyValuePair variable. The key is
the current itemCount ID and the value is its amount. After this, we stop the for
loop with break , so that we no longer iterate through our inventory.
If the current invItem variable is empty, we add the new GameObject to our invent-
ory. We do this by creating the same val integer as we did recently for our amount,
and assigning the value of the current itemCount value plus the amount of the item
that we want to add. Then, we assign current invItems GameObject to NewItem ,
the GameObject passed into the function.
Search WWH ::




Custom Search