Board Thread:General Discussion/@comment-24454571-20140204220440/@comment-24854750-20140429143247

When I started playing Petit Computer, Arrays, DIM, RESTORE, DATA, and READ weren't very easy to understand at first, but I think I've got it figured out. So I'll try to share what I know.

Arrays are a lot like variables except that each array can store multiple variables. This is important to understand for when you setup and use an array. First, you have to decide how many variables you want your array to hold (for now, we'll say 3), and then program the following: DIM HORSE(3). That command will setup an array of 3 variables called HORSE (because like variables, you can call an array whatever you'd like). From there, you can assign values to the array just like you might to a variable. An important note though: if, like in the above example setup, you want three variables, the array can only be defined from 0 to 2. So if you were defining variables for HORSE, you might program it like this:

HORSE(0)=9:HORSE(1)=16:HORSE(2)=25

From there, you can use those in the same way that you use variables.

Something else I should probably include is the FOR, TO, NEXT commands. These are easy to understand and use, and they are useful for defining arrays. Basically, you would use them to say that FOR some variable equal to some value TO some other value, do this, then do the same thing with the NEXT number. For example:

DIM HORSE(3) 'setting up the array

FOR A=0 TO 2 'using A=0, A=1, and A=2

HORSE(A)=(A+3)*(A+3) 'define the array

NEXT 'tells the computer to use the next number in sequence

Now the array HORSE array is equal to the values I used earlier.

DATA, like arrays, can store multiple values, but setup and usage is different. With DATA, you can just string out the values after it (such as: DATA 0,4,16,42). To access DATA, you will need a label and the RESTORE command. To use the values written with DATA, the READ command can save those values to variables. So:

RESTORE @PIG 'tells the computer to use the DATA defined at the PIG label

READ A,B,C,D 'saves the values defined in DATA to variables

PRINT  A,B,C,D 'displays the values defined in DATA

@PIG

DATA 0,4,16,42

Based on what research I did, the most common use for all of these commands is constructing backgrounds. I could write out an example, but I'm afraid I'll beat your longest post record. So I'll just recommend downloading my game Escape, which uses all of these things.