Dim is used to create an array. An array is like a collection of variables, or a list. It is best used when you have a known number of items in the list.
For example, say you have an RPG and you have a string variable containing the names of character classes. You know you have only 5 possible classes.
I am sure you know how to declare a standard string variable... the only difference is that an array needs to be 'dimensioned' (which essentially sets aside the memory for the entire array).
You would use the statement
Dim characterClass$(5)
This is essentially creating 5 (Well, actually 6 but I'll explain that later) different string variables, in a list, like such:
characterClass$(1)
characterClass$(2)
characterClass$(3)
characterClass$(4)
characterClass$(5)
you can then assign unique values to each of these members of the array.
characterClass$(1) = "Fighter"
characterClass$(2) = "Cleric"
characterClass$(3) = "Mage"
characterClass$(4) = "Paladin"
characterClass$(5) = "Thief"
Now, for your character in the RPG, you don't need to know the actual text description of the class, you only need to know the NUMBER. Say your character is a paladin, you just need to use a 4 to get the proper element of the array.
myCharacterClass = 4
Print "My character is a " + characterClass$(myCharacterClass)
Will print "My character is a Paladin." Note you can use another (integer only) variable as the
subscript or
index of the array. This is where arrays really become powerful.
Some notes:
Arrays should be DIMed fairly early in your code, as they cannot be dimensioned within a function, and they are ALWAYS GLOBAL.
You can fall into a bad habit with Blitz, like I did up above. In almost every other programming language, with a few exceptions (VB6 being one of them) Arrays are
zero-based. This means for a 5 element array, you should actually Dim it like array(4), and then start the list at array(0). With Blitz, however, you can do it like I did it above, and just not use the (0) index position.
Do not let your program reference the array beyond the limit, it will cause your program to crash. (For instance, in the example above, if you tried to reference characterClass$(6) )
Arrays can be multi-dimensional... using 2, 3 or any number of dimensions. This is great for using say, a puzzle grid, a lookup table, high score table, 2d tilemap data and so on. I can describe this in more detail if you would like.
To sum up, an Array:
1. is a numbered LIST of one type of variable
2. needs to be DIMensioned to an exact size
3. should be dimensioned near the beginning of your code
4. is always GLOBAL in scope
5. each member can be referenced by its corresponding index or subscript
6. you can use another integer variable in place of the subscript
7. can have multiple dimensions
8. the index of the array cannot be lower or higher than the dimensioned amount
Limitations:
If you do not know the exact number of members of the list, you need to use another method, such as
TypesThere is another kind of array called a Blitz Array. The docs are murky on this, so I won't bother explaining them. The main difference with them is they are zero-based, can only be one dimension, are local in scope and can be passed to a function.
Arrays in Blitz cannot be passed to a function, however, since they are global they can be used from inside a function, but they must be dimensioned outside of a function. This is actually considered bad form from a structured programming standpoint, but hey, we're just making games here, right?