Imagine a scenario where you have a four player game. When every player's score hits 100 the game would end. Using ordinary variables the code to detect whether all four players have reached a score of 100 might look like this.
player1 as integer = 0 player2 as integer = 0 player3 as integer = 0 player4 as integer = 0
if player1 = 100 and player2 = 100 and player3 = 100 and player4 = 100 // finish the level endif
The code handles all four players, but imagine if the game suddenly expanded. What would happen if the game design was changed so that eight players could play in each level? You could declare four more variables to store the score for the extra players. You would also need to update any code dealing with the score to take into account the new players. This approach works, however, it's not very flexible as having to deal with changes every time more players are added is far from ideal.
This scenario is quite simplistic, but it does demonstrate the need to be able to handle data in another way, which is where arrays come in. Arrays can be thought of as a container of data, allowing you to store and access data through one variable. Instead of having eight separate variables to store the score for the players we could instead have one variable, resulting in our code being much easier to deal with.
The way in which an array is declared is essentially the same as regular variables. Let's take a look at how an array can be declared to contain our scores for eight players.
playerScores as integer [ 8 ]
The key difference to a normal variable is that at the end of our declaration an opening bracket is used, along with a number and then a closing bracket. These three components let AppGameKit Studio know that this variable is an array and will have eight parts or elements.
To set data within an array you need to use the square brackets and pass in an index that controls which part of the array you want to deal with. The code that follows shows how each part of playerScores can be assigned values.
playerScores [ 1 ] = 10 playerScores [ 2 ] = 20 playerScores [ 3 ] = 30 playerScores [ 4 ] = 40 playerScores [ 5 ] = 50 playerScores [ 6 ] = 60 playerScores [ 7 ] = 70 playerScores [ 8 ] = 80
Our array has eight parts, so to access part 1 use an index of 1, for part 2 an index of 2 etc.
This program shows how the values of the array can be displayed on screen.
playerScores as integer [ 8 ]
playerScores [ 1 ] = 10 playerScores [ 2 ] = 20 playerScores [ 3 ] = 30 playerScores [ 4 ] = 40 playerScores [ 5 ] = 50 playerScores [ 6 ] = 60 playerScores [ 7 ] = 70 playerScores [ 8 ] = 80
do for i = 1 to 8 print ( playerScores [ i ] ) next i
sync ( ) loop
An alternative and more efficient way of writing this program would be to alter the way in which the initial values are assigned to playerScores by using a for loop.
playerScores as integer [ 8 ]
for i = 1 to 8 playerScores [ i ] = i * 10 next i
do for i = 1 to 8 print ( playerScores [ i ] ) next i
sync ( ) loop
What we end up with is a container variable that stores eight values, which in this instance is far more useful than having eight separate variables to store the data. It results in less code that is more manageable and easier to expand. If, for example, we decided to change the number of players it would simply be a case of altering the declaration of the array and using a different value, meaning our code is much more dynamic.