|
Arrays |
|
Arrays are very useful. They are essentially a way to store many values under the same name. Think of an array like a table (not the kitchen furniture, the other one!).
That information could be but in to an array.
player_name[0] = "bob" player_name[1] = "joe" player_name[2] = "bill" player_name[3] = "mary"
That is one way of using an array, to simply store a large amount of information under the same name. Notice that the first value (called an index) in an array (in GML) is 0. These are 0 based arrays, so item 1 in the table is assigned to player_name[0].You can also think of these arrays like this:
[] [] [] [] []
ie. A series of slots to put information in. However, going back to the table analogy, what if the table has multiple columns?
Well for this, we would use a 2d (2 dimensional) array.
player_data[0, 0] = "bob" player_data[1, 0] = "joe" player_data[2, 0] = "bill" player_data[3, 0] = "mary" player_data[0, 1] = "192.168.1.1" player_data[1, 1] = "127.0.0.1" player_data[2, 1] = "1.2.3.4" player_data[3, 1] = "255.255.0.0"
If a 1d array looked like this:
[] [] [] [] []
A 2d array would be represented like this, with slots along and down:
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
It is worth mentioning that unlike more rigid languages, you don't have to say how big your array is going to be. You simply start assigning values to it like a variable. GM has an internal limit of 32000 for the index. This means you can have an array from 0 to 31999, but 32000 will cause an error. There is also a limit on the total size of an array, which is 1000000. This means that a 2d array that is from [0,0] to [999,999] is valid (exactly 1000000 "slots" inside this array), but an array of [0,0] to [1000, 1000] is invalid (more than 1000000 "slots" in this array)
Example In an RPG game you want to store the value of different items for trade, you might consider this approach.
longsword_value = 100 pie_value = 10 mushroom_value = 5 arrows_value = 20
Which does the job, but it could also be stored in an array like this.
value[0] = 100 value[1] = 10 value[2] = 5 value[3] = 20
That way is undoubtedly neater, but where are the advantages? And also, how are you supposed to remember what index of the array is used for what item. Well here constants (hyperlink) are a good idea. In the global game settings, you can add constants to the game. So you could add the following constants.
Which means you can assign values to your array like this.
value[longsword] = 100 value[pie] = 10 value[mushroom] = 5 value[arrows] = 20
That easy to read isn't it? Doing things like this makes your code very easy to understand, which is essential for large scale projects. Another advantage of arrays is that it is possible to iterate through them. I will do so with a for loop and find out some information.
// make some temporary variables to store the data in for this script var lowest, lowest_index, highest, highest_index, total, i;
// set the lowest to a high value, and the highest to a low value lowest = 9999 highest = 0
// set the total to zero total = 0
// iterate for(i=0;i<4;i+=1) { if value[i] < lowest { lowest = value[i] lowest_index = i } if value[i] > highest { highest = value[i] highest_index = i } total += value[i] }
this code would iterate through the array "value" from index 0 to 3. the highest and lowest value and index are stored to the variables named as such as well as the total. This in its self probably isn't particularly useful, but it serves as a good example of how you can manipulate arrays in advanced ways.
Now suppose we need to store a lot of information about some different objects. Say we need to store the cost, width + height (for inventory), weight and minimum level requirements.
You might think of doing it like this
value[0] = bla width[0] = bla height[0] = bla weight[0] = bla mlvl[0] = bla
Which is fine, but we can also use 2d arrays to solve this problem. Add some more constants to your game:
Now we create a 2d array to store all of our item information under the same name.
item_info[longsword, value] = 100 item_info[longsword, width] = 2 item_info[longsword, height] = 4 item_info[longsword, weight] = 80 item_info[longsword, mlvl] = 4
if you were to use individual variables for this, imagine the mess it would be. As you progress with the creation of games that require data to be stored and processed, you will realise the benefit of arrays over individual variables.
monkey dude - Revision #1 |