Game Development Reference
In-Depth Information
Adding the Player inventory behavior
Now that we are done with the whole inventory saga, we need to ensure that when the
player buys a weapon, they are able to use it and defend themselves when attacked.
There are a couple of ways of doing this as follows:
• Updating the player's statistics on acquiring the equipment
• Having assigned slots that affect the player's statistics
• All items in the inventory affect the statistics and are queried during the battle
The first step is the simplest as it requires no additional UI, which we will implement in
the next code.
Open up the Player script under Assets\Scripts\Classes and add the following
additional function:
public void AddinventoryItem(InventoryItem item)
{
this.Strength += item.Strength;
this.Defense += item.Defense;
Inventory.Add(item);
}
The preceding code gives us a single point to control how the player's statistics are affec-
ted when we grant them a new inventory item in the game.
Tip
Ideally, you would want to add further control to this function, including making the play-
er's Inventory property read only to avoid accidental direct access, which would bypass
the calculation of the player's statistics.
Now that we have our helper function to control a player's Inventory property, we just
need to update the ShopSlot script we created earlier to use the following new function:
public void PurchaseItem()
{
GameState.currentPlayer.AddinventoryItem(Item);
Item = null;
Search WWH ::




Custom Search