Arrays

Arrays are data structures that can contain more than one piece of data of a single data type.

Uses of Arrays

Arrays hold multiple pieces of data. They are useful when any data structure is needed to hold a list or table of data.

Some of the most common uses of arrays in game programming are :

  • Holding a list of game objects such as bullets. This makes the game objects much easier to manage.

Additionally, 1D arrays can be used to :

  • Hold and shuffle the data for a deck of cards.
  • Hold the highscores of a game.
  • Hold the individual frames of an animation for a sprite

Creating Arrays

Arrays must be created using Dim. (Creating an array is sometimes called 'declaring an array' by programmers)

The example code below creates an array with 5 elements.

Dim myArray(6)

When an array is declared, it has elements to hold data, but no actual data in the elements. A string array is created with an empty string (the same as myArray$(1) = "") while numeric array elements are created with a value of 0.

Array Types

There are three major kinds of array types. Integer, String, and Float. For more information about strings, integers and floats, see Data Type.

The example code below creates an array of each type :

Dim stringArray$(5)
Dim integerArray(5)
Dim floatArray#(5)

Array Index

The array index is the number used to tell which element of the array is being referred to. The array index is always enclosed in parenthesis ( ).

For example :

myArray(5) = 42

In the above code example, the array index is 5.

Negative Array Index

Multi-Dimensional Arrays

2D Arrays

Arrays that have two dimensions (and therefore two array indices) are called 2D arrays.

Uses for 2D Arrays

The uses for 2D arrays are many. By their very nature, they are a grid and therefore are good at storing any kind of data that requires a grid.

Some uses of 2D arrays :

  • A bingo card could be randomly generated an stored in a 2D array.
  • Storing the current state of a game of Tic-Tac-Toe (Naughts and Crosses)
  • Storing the level data (in tiles) of a 2D game : see Tilemap
  • Storing layout of a 2D maze
  • Storing enemy spawn point locations.

Create a 2D Array

The example code creates an array with 5 elements in two dimensions.

Dim myArray(5,5)

3D Arrays

3D arrays have one more array index than 2D arrays.

Uses for 3D Arrays

Because 3D arrays have one more array index, they are useful for grouping together 2D arrays :

  • In a 2D game, holding the not just the level data, but powerup and enemy positions as well.

Random Arrays

Random arrays come in a few general flavors :

  • a table of data from which something random is chosen
  • an array filled with random data
  • a table of data which is then shuffled, or randomized

Arrays Beyond 3D

Arrays beyond 3D arrays can be declared and used in PlayBasic. Unfortunately, we honestly can't think of a use for anything above a 3D array.

  • One possibility : Store all level data for a large RPG in a single array.

Filling Arrays

Filling an array is when multiple values (normally all elements) are assigned to a value.

Sorting Arrays

PlayBasic has a function to sort arrays : SortArray

Searching Arrays

Array Pointer

Related Pages

How To

Reference

Array Functions

Source Code

Links

Categories: Arrays : Data : Data Structures : PlayBasic