Creating the inventory script

For our inventory, we'll only use one script, so let's get it started.

Creating and naming the script

The first thing we need to do is to create a new C# script and name it Inventory. When you open the script, delete the Start and Update functions, leaving an empty class for us to use.

Adding the necessary variables

First, add this using statement where the other using statements are. The using statement will be needed so that we can use the List container variable:

using System.Collections.Generic;

Now, let's add the variables we require and place them after the opening class defining bracket:

bool showInventory = false;
public Rect inventoryRect = new Rect(Screen.width / 2, Screen.height / 2, 400, 400);
public GameObject EmptyObject;
public int InventorySize = 9;
public GameObject[] invItems;
public GameObject[] QuickItems;

List<KeyValuePair<int, GameObject>> items = new List<KeyValuePair<int, GameObject>>();

List<KeyValuePair<int, int>> itemCount = new List<KeyValuePair<int, int>>();

The showInventory variable will be used when we activate an input. This is how we determine when to and when not to show the inventory GUI. Next, we have a Rectangle variable, that we'll use to determine where we put the inventory GUI and what size it should be. By default, we set the X and Y positions to the center of the screen.

Our next variable is a GameObject; in the Inspector panel we will set this as an empty object. We'll be using the empty object in our inventory as a placeholder when there is no item to be placed there. The next variable, aptly named InventorySize, will determine the size of our inventory.

The next two variables are GameObject arrays. We use these to hold the actual GameObject items that we will hold in our Inventory. InvItems will hold the GameObjects that are in our inventory and QuickItems will hold the GameObjects that the player wants as their quick-items.

Lastly, we have two lists for our final variables. The first one will hold the GameObject items within our inventory; the list is made up of KeyValuePairs. The key will be the ID of our item and the value is a GameObject, which is the item in our inventory.

Our next list is also made up of KeyValuePairs and both the Key and Value are integers. The Key will be our ID that will match itemCount to the correct inventory item. The Value will be the actual number of items that we have.

Initializing our inventory

Time for our first function! This method will be used to create our inventory for the first time.

Creating the initializer

Let's create our initializer by adding the following function to the script:

void InitializeInventory()
{
  invItems = new GameObject[InventorySize];
  for(int i = 0; i < InventorySize; i++)
  {
    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 InventorySize 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()
{
  InitializeInventory();
}

We have created our inventory. It's currently an empty inventory, but all the necessary containers have been created and assigned to default values. Since we used an iterator variable for all the keys and arrays, all the values in the containers coincide with each other. We also created and assigned our quick items to empty GameObjects, just as we did for our inventory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.48.161