Storing data in an array, a List, or a Dictionary

There are times that many items need to be stored in some type of list. Perhaps a selection of weapons that a character may use. An example used later in this book is a list of splashscreens for the State Machine project we will build.

There are basically two ways to access items in a list:

  • Direct retrieval: The location of an item in the list is already known, so code is written to access it directly, or
  • Loop retrieval: The location of an item in the list is not known, it's just in there somewhere, so code is written to loop through the list until the item desired is found.

First though, we need a list of items before we can select anything from the list. An example of collecting items into a list, then looping through the list, is shown in the Scripting Reference under the GetComponen ts() method:

public HingeJoint[] hingeJoints;
void Example() {
  hingeJoints = gameObject.GetComponents<HingeJoint>();
  ...
  }

All the HingeJoints in a GameObject are collected into an array (list). Once all the HingeJoints are in the array, it's up to us to decide which HingeJoints we want to work with in our code.

So bottom line, what are we talking about here? We know that a variable stores a single item. For instance, we could store a single weapon in a variable. That's great as long as we only have one weapon. Suppose we have the option of using many different weapons. To store these weapons we would need a separate variable for each one. A better option would be to store all the weapons in some sort of super-variable that can store many items, that way they're all stored in one place, not in a whole bunch of different variables.

That's what an array, a List, or a Dictionary is, a variable with the ability to store many items. Like a super-variable divided into many cubbyholes.

Storing items in an array

Looking at the GetComponents() example on the Scripting Reference, let's see how an array is created:

  • As per the code on line 1: public HingeJoint[] hingeJoints;
    • public means this array will appear in the Inspector. Also the array is accessible from other scripts.
    • HingeJoint[] is the type of variable being created. It's going to be a HingeJoint type (HingeJoint is a class in the Scripting Reference).
    • The square brackets specify that the variable created is going to be an array, a variable with many cubby-holes to store several HingeJoint objects, and only HingeJoint objects.
    • hingeJoint is the name of the array being created.

    That was easy enough. It's just like creating any other variable. The only difference was the addition of the square brackets to specify that the type of variable being declared is actually going to be an array.

    Now that the array is created, the GetComponent() method retrieves all the HingeJoints on the GameObject and stores each of them into the array:

  • As per the code on line 3: hingeJoints = gameObject.GetComponents<HingeJoint>();
    • hingeJoints is the array
    • GameObject is the variable that stores the GameObject this script is attached to
    • GetComponents<HingeJoint>() is the method used to find every HingeJoint object on this GameObject

As each HingeJoint object is found, it is stored into one of the cubbyholes of the array. These cubbyholes actually have a real name called an element. These elements actually have a specific location inside the hingeJoint array. Each element is given an index number. The first HingeJoint found would be stored in the element at index 0, the second one found is stored in the element at index 1. The third at index 2, and on and on until all the HingeJoints are found on the GameObject.

So if we knew exactly, which HingeJoint in the array we wanted to work with, perhaps the second HingeJoint which is stored in the element at index 1, we can simply retrieve it directly by saying it's stored in the variable:

hingeJoint[1]

Once again we use the square brackets because the variable is actually an array, and also to specify the index number.

Note

Please notice that the very first index number starts with zero. This is called zero indexed. It's just something you will have to remember. Many things in programming are zero indexed, and it creates coding errors when you forget, especially for beginners.

That's all I want to say about using arrays to store objects because I want to discuss using a List instead. It's like an array with extra benefits.

Storing items in a List

Using a List instead of an array can be so much easier to work with in a script. Look at some forum sites related to C#, and Unity, and you'll discover that a great deal of programmers simply don't use an array unless they have to, they prefer to use a List.

Here are the basics of why a List is better, and easier, to use than an array:

  • An array is a fixed size and unchangeable
  • The size of a List is adjustable
  • You can easily add to, and remove elements from a List
  • To mimic adding a new element to an array, we would need to create a whole new array with the desired number of elements, then copy over the old elements

The first thing to understand is that a List has the ability to store any type of object, just like an array. Also, just like an array, we must specify, which type of object you want a particular List to store. This means that if you want a List of integers, of the int type, then you can create a List that will store only the int type. Want a List of pony names? Then create a List that will store only the string type.

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

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