How to Fake an Array

by Al Staffieri Jr.

Several people asked if arrays can be used in GameMaker. Here's a way to fake it depending on your needs (got this idea from IMs with FlairInt and FXwzrd).

This example lets you have an array of 10 elements with each element able to be a number from 0 to 99. You create a string of 20 characters. Every 2 charcters represents another element.

So if you want a game where you have to collect 10 crystals to win the game, instead of having 10 variablkes like this:

crystal1 = 0
crystal2 = 0
crystal3 = 0
etc.
you could use just one string variable (although you'll end up using several other variables as well which defeats some of the purpose). Below is just an idea. I'll try to make a simple game using this idea and upload it as open source.

' set an array in another script. It should look like this:
' array$ = "00000000000000000000"
' this uses every 2 characters as an array element.
' to set an item in the array make getsetarray = 1.
' to see what the value of the item is, set getsetarray to 0.


IF getsetarray = 1 THEN
USERASK Which array do you want to get (1 to 10)
value = val userask$
value = value * 2
value = value - 1
temp$ = mid$ array$ value 2
myNumber = val temp$
PRINT $myNumber$
ELSE
USERASK Which array do you want to Set (1 to 10)?
arrayNum = val userask$
value$ = USERASK Set the value of the array.
'value = val userask$
arrayNum = arrayNum * 2
arrayNum = arrayNum - 2
temp$ = left$ array$ arrayNum
temp$ = temp$ + value$
arrayNum = arrayNum + 3
newtemp$ = mid$ array$ arrayNum
temp$ = temp$ + newtemp$
array$ = temp$
PRINT $array$$
END IF