Using Arrays In DnD


Using Arrays In DnD

In this tech blog, we're going to explore arrays and how to use them when working with Drag and Drop™. Before we start showing you how to use arrays, we'll first explain what they are and why you'd want to use them.


WHAT IS AN ARRAY

So, what is an array? Essentially, an array is a single variable that can hold multiple values, with each value being identified by an integer number between square brackets [] placed after the variable name. The number should start at 0 and increment by one for every value you want the array to hold, like this:

array[0], array[1], array[2], ... array[99]

Being able to have a single variable with multiple values like this makes arrays ideal for a number of tasks, for example:

  • Player values. Arrays can make tracking and maintaining general player values much easier, as they do away with the need for a jumble of different variables.
  • Card Games. Good for keeping track of cards and hands.
  • High scores and other statistics. Much easier to keep track of one array than multiple variables.

Those are just some examples of when you can use an array, but there are many, many more!


CREATING AN ARRAY

Arrays are created using the variable actions from the Common section of the DnD toolbox, and just like any other variable, an array can also be local, instance or global in scope (for more information on variable scope see here. In this tech blog we'll just be using instance-scoped arrays.

We'll start by creating an array to hold various player values like health, score and mana:

Declare an Array using DnD™

In the above image, you can see we've declared a variable "player" and we've given it three values numbered from 0 - 2 using the square brackets []. This is a basic 1 dimension array (arrays can have more than one dimension, but we'll get to that later in this article). You aren't limited to how many values an array can hold, and all values are indexed consecutively.

You can then perform operations on the array, just as you would any other variable. Here are some examples:

Operations on Arrays

NAMING ARRAY POSITIONS

When using arrays, it can sometimes make sense to have numbers for each of the positions - for example, a high score array with values numbered from 0 to 9 - but when working with other contexts it can be hard to keep track of exactly what each position in the array represents. Consider the example we gave above of a player array for health, score and mana, but imagine that we expanded that to also include ammunition, gamepad index, sprite index, image index or any number of other things. The more values that an array holds, the harder it would be to track each one and the easier it would be to target the wrong array value in operations. To help with this, you can create macros for each of the values. A macro in this context is simply a named value constant that will never change throughout the game.

Creating a macro is easy, and requires you to use the Macro action, like this:

Create A Macro To Name Array Positions

The important thing to note here is that each macro should be a value consecutive with the previous, and once created you can then use them instead of numbers to identify an array position. Our "player" array declaration will now look like this:

Using Macros To Name Array Positions

As you can see, this makes the array far more readable, and it reduces the possibility that you will target the wrong array position when performing operations on it later.


2 DIMENSION ARRAYS

Arrays can have either 1 dimension or 2 dimensions. If you think of a one dimension (1D) array as being like a list, then a 2 dimension (2D) array can be thought of as being like a grid, where the first dimension is the row and the second is the column. 2D arrays can be created and used exactly like 1D arrays - for example, here we'll initialise a 2D array "inventory" to have 9 different "slots" (positions):

Create a 2 Dimensional (2D) array

As with 1D arrays, you can perform operations on the different array positions and can also use macros to name them.


ADVANCED ARRAY FUNCTIONALITY

One of the great things about arrays is that since they are numbered consecutively, you can use loops to set and get values from them. Consider an array for high scores... You want to initialise a high score array with 20 positions, so you can either use multiple Set Variable actions or you can use a For Loop, something like this:

Create a 1D Array Using a For Loop

Here we create two arrays, one for the score and one for the name associated with the score. You can then use another loop to draw the values from the array:

Draw a Highscore Array Using For

Another thing you can do with arrays is pass them through to scripts to be used and then returned to the instance that called the script. To do this, you simply have to specify the array variable (no need for each of the individual entries, nor the [] brackets) and the array will be passed by reference into the script. However, should you change any of the array values, the array will be copied into a temporary array just for the script. Note the use of the word "temporary" here! You are not actually passing the array itself into the script (as you would a variable), but instead, you are requesting that the script create a copy of this array, which you will change in the script. This means that you must always return the array from the script if you wish to change any array values.

Let's see an example of this in action. We'll create a script set_score(array, position, score):

Simple Script To Set An Array Value

We would then call the script like this:

Using The Array Set Script

Notice how the script returns the temporary array and in the Execute Script action we Target the same high score array we used as the first argument. Obviously, if you aren't changing anything in the array during the script which you want to be preserved (i.e.,: you're just reading from it, or you're happy to lose any changes) then this step of adding a Return action and setting a Target isn't necessary.

I think that about wraps up this tech blog, and we hope that we've shone some light on how to use arrays in your games when you're creating them using Drag and Drop™!



Written by Mark Alexander

Mark Alexander is the former GameMaker technical writer. He’s moved on to pastures new, but still hangs around as admin on the GameMaker Community forum and makes his own indie games as Nocturne Games.