Storing data in Construct 2

This is usually the time when I teach you to destroy the enemies and add scores to the game. However, because the focus of this chapter is to store data from the game, we'll cover this first. There are four objects we can use to store data in Construct 2: array, dictionary, WebStorage, and XML. We're not going to cover XML in this book, because XML is a bit hard for beginners of programming, but only knowing the first three is enough for you to save data from your game.

Getting to know arrays

An array, in programming context, is a list of ordered things. An array can be a list of all text values, all number values, or a combination of text and numbers. In programming languages, arrays are written as a list of data inside a square bracket; so, in this book, I will also write an array in such a way. For example, an array of countries will be ["Canada", "USA", "Russia", "Italy", "Germany", "England"], while an array of numeric values will be [100, 250, 350, 150, 600, 450].

One thing to keep in mind when dealing with arrays is that arrays are zero-based indexed lists, which means that they start with 0 instead of 1. So, in the earlier country list, "Canada" is array member zero, and "Italy" is member number three. Remember this because this is a mistake beginners often make.

Understanding the elements of an array

By default, Construct 2's array contains 10 empty members. Members are elements or values that are stored in an array. Taking our countries array as an example, we can say that the members of the array are as follows:

countries[0] = "Canada"
countries[1] = "USA"
countries[2] = "Russia"
countries[3] = "Italy"
countries[4] = "Germany"
countries[5] = "England"
countries[6] = empty
countries[7] = empty
countries[8] = empty
countries[9] = empty

When you want to add another member to the array, you have to add it between the ranges of the total number of elements (I'll explain this later in the Using arrays in your game section). So, you can only add to countries[6] through countries[9]; trying to add an element to countries[11], for example, doesn't do anything. It doesn't change the array at all. If you want to add more members than the total number of elements of the array, you must first change its length.

A length is the total number of elements the array can hold. An array of 10 members means that the array has a length of 10. To add the eleventh or twelfth members to an array, we must first change its length.

Note

Arrays can only contain a set number of data (it's 10 by default) called a length. Arrays don't automatically increase their length; it must be done manually. You can only add new members to the array if it is within its length.

To change the length of an array, we only need to change its value in the Properties bar. In the following screenshot, Width is the length of the x axis of an array, Height is the length of the y axis, and Depth is the z axis. More about these axes will be explained in the next section.

Understanding the elements of an array

One-dimensional and multidimensional arrays

What I'm about to cover here is a little difficult, but since this is a feature of an array and the array object in Construct 2 supports it, I have to teach you about it. This is for advanced readers; you can skip this part if you want to. There are two kinds of arrays:

  • One-dimensional arrays: These are arrays that only contain one-dimensional data on the x axis. An example of a one-dimensional array is the list of countries and numerical values that I mentioned earlier.
  • Multidimensional arrays: These are arrays that contain data in more than one dimension. A two-dimensional array can be imagined as a table where the data is stored in its x and y axes.

    An example of this is a table that contains the stats of every enemy, which looks as follows:

    One-dimensional and multidimensional arrays

The other multidimensional array is a three-dimensional array where data is stored in the x, y, and z axes. This can be seen when you store data in a cube-shaped storage. In general, you probably don't need to use a multidimensional array in your game. For simple games, a one-dimensional array is enough to store player data.

Storing data in a dictionary

A dictionary is pretty similar to an array; this means that it can also store text and numerical values. However, there are two differences in a dictionary: it doesn't order its members, and it stores values associated with keys. An array has the first member stored in index number 0; it also has a second and third member stored in index numbers 1 and 2, respectively, and so on. A dictionary doesn't have ordering like this.

A dictionary stores its values associated with keys. Keys are strings of text used to access a value. So, for example, we store a value of Joe with the username key, and a value of 250 with the scores key. Retrieving the username key will give us Joe, and retrieving scores will give us 250. Remember that keys are case sensitive, so username and UserName store two different values.

Comparison between arrays and dictionaries

Now, you know that arrays and dictionaries are pretty similar, with a few differences as follows:

Arrays

Dictionaries

The positions of the members are ordered

The members have no ordering; they are associated with a key

To retrieve a member's value, we will use its index position

To retrieve a member's value, we will use the key that's associated with the value we want to retrieve

Can be sorted

Can't be sorted

Allows complex data storage with multidimensional arrays

Allows only simple storage with keys

Noting these differences, there are situations where we use arrays and where we use dictionaries. We will use arrays when we:

  • Want to be able to sort the data, whether ascending or descending
  • Want to retrieve the value by some mathematical rules; for example, we might want to see members 1 to 5 and not all
  • Store complex data using multidimensional arrays

We'll use dictionaries when we:

  • Want to create a list of values we can associate with a name.
  • Want to retrieve a value based on text; for example, we can create an upgradePoints dictionary and store a value with a Hero1 key. When we want to get the upgrade points for Hero1, we can just call the dictionary with the Hero1 key.

These are just examples; you can find your own uses based on your creativity.

Using arrays in your game

Alright, I have explained a lot about arrays and dictionaries; now is the time to put them to use. First of all, let's create a global variable named scores with type number; we'll use this to store our scores. After this, let's add an array object to the game project; array objects will be added to the whole project. Name this object scoreArray.

We'll increase the scores variable by 10 every time an enemy ship is destroyed; we'll destroy an enemy ship every time it is hit by the player's lasers. Usually, in other shooter games, the enemies will have health that decreases until they're destroyed, but in our case, we'll destroy our enemies with one shot. So, we'll edit the event where playerBullet hits an enemy, and then, we'll add one more event when the enemy is destroyed.

Using arrays in your game

We have added a value to the scores global variable, but we haven't added it to our array yet. There are two ways of adding an element to an array:

  • Pushing it: Push is the usual command in traditional programming languages when you want to insert an element to an array. There are two kinds of pushes: push back or push front. Push back is inserting an element to the last index of an array; for example, if an array of 10 empty elements is pushed back, then the new member is on index 9. If it is pushed back again, then the first element is on index 8, the new element is on index 9, and so on. Push front is the same as push back, but the new element is pushed to the first index of an array.
  • Setting the value at a location: Setting the value at a location is easiest to understand, because it doesn't change the position of another member of the array unless it's at the same position. So, using the countries array that we saw earlier, we know that index numbers 6 to 9 are empty. Adding China to index number 7 doesn't change the position of other elements; it simply adds a new value. The only important thing to remember here is that it will erase the old value if the index position specified already has a value. So, for example, if we insert Japan to index 2 in our pervious countries example, it will erase Russia and replace it with Japan.

We'll let players collect as many scores as they can before we store the scores to our array. We'll do this when the player is destroyed, so let's edit the event where the enemies' bullets hit the player and add another event for when the player is destroyed:

Using arrays in your game

The code for the On destroyed event is as follows:

Using arrays in your game

This will set the value of scoreArray at index 0 to the scores variable. When setting the value, you might see that there are set at XY and set at XYZ actions. We will use set at X. The other two are used when you insert a value to a multidimensional array that I explained earlier.

Inserting data into a dictionary

Now, let's try to do the same thing with a dictionary. First, we will add a dictionary object to our game. Just like an array, a dictionary is added to the whole project. Add a new dictionary object to our game and call it scoreDictionary.

Usually in a game, you use either an array or a dictionary, and not both, but we already have an event here that uses an array. We can just delete it before adding our code snippets for when we use a dictionary, but here, I'll show you how to disable code. First, select the event that you want to disable; for us, this will be the event where the player's ship is destroyed, and we will set a value to an array. After that, press the D key, and the event(s) and action(s) will be disabled, or you can right-click and select Toggle Disabled. Disabled events and actions are marked with a strikethrough over their text, as shown in the following screenshot:

Inserting data into a dictionary

Tip

There is more than one way to disable an event:

  • You can disable an event by selecting it and pressing the D key
  • You can disable multiple events at once by selecting the ones you want to disable and pressing the D key
  • You can disable one or more actions in an event without disabling the event
  • Selecting a disabled event/action and pressing the D key will re-enable that event/action

Now, let's add our code to add a new member to the dictionary. When adding a new value inside a dictionary, we will define the key along with it. We're storing the user's score here, so we'll simply call the key score:

Inserting data into a dictionary

Retrieving the value in an array and dictionary

We now know how to store data to our array and dictionary, but what good is storing data without knowing how to retrieve it? I'll show you how to do this with both objects. Before that, we'll create a place for them on the screen. Let's create two text objects: one is called arrayScoreText, and the other is dictionaryScoreText; place them in the upper-left corner of the screen as follows:

Retrieving the value in an array and dictionary

Then, in the event sheet, we will add actions to show their values in the respective text objects. For this, we will use two On player destroyed events: one to retrieve the array's value and another to retrieve the dictionary's value. To retrieve the array's value, we will use an expression that gives us the value of the array's member, based on the index number we supply to it. This expression is called an At() expression, and we will supply the index number between its brackets. We'll add the value we get from it to the text object:

Retrieving the value in an array and dictionary

Next, we'll retrieve the value from the dictionary. To do so, we need help from an expression, but instead of an index number, this expression needs (or takes) the key of the value we want to retrieve. We'll add the returned value to the appropriate text object:

Retrieving the value in an array and dictionary

Test the game now, and you will see that the game shows the score the players get after they die.

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

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