| value - a number or a quoted string to store; must be a constant, not a variable or a function call |
|
Stores a list of fixed values inside the program for Read to fetch. Data is where you keep the tables a game is built from: the layout of a level, a list of enemy speeds, the words of a dialogue, the default high scores. The values sit in the source rather than in a separate file, so there is nothing to load and nothing to go missing. Every Data statement in the program joins one long list, in the order the lines appear in the source, however far apart they are. Read takes the next value off that list and moves on; Restore moves the position around. Types can be mixed freely - Data "Grunt",50,1.5 is fine - because each value is converted to suit the variable you Read it into. A string of digits read into an integer is converted for you, and a number read into a string arrives as its text. Mark the start of each block with a label so you can jump to it: .level1 Data 1,1,1,1,0,0,1,1 Data 1,0,0,0,0,0,0,1 Then Restore level1 before the loop that reads it. That is what makes Data blocks worth using for several levels at once. Two limits. Data lines can only appear in the main program, not inside a Function, although a function may still Read from them. And every value must be a constant the compiler can work out - "Data expression must be constant" - so no variables and no function calls. Reading past the last value raises "Out of data", so either count exactly or finish each block with a marker value the loop watches for. See also: Read, Restore, Const, Dim. |
; Data Example
; ------------
; Data statements hold constant values - here, a whole game level.
; Point the Data pointer at the label, then Read the values in order.
Restore level1
; The first value is the level name, then 5 rows of 10 tiles
Read level_name$
Print "Loading level: "+level_name$
Print ""
; 1 = wall, 0 = floor - build each row into a string and print the map
For row=1 To 5
line$=""
For col=1 To 10
Read tile
If tile=1 Then line$=line$+"#" Else line$=line$+"."
Next
Print line$
Next
Print ""
Print "Press any key to close the example"
WaitKey
End
; The level, drawn with Data - easy to read and easy to edit
.level1
Data "Asteroid Cave"
Data 1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,1,0,0,0,1
Data 1,0,0,0,0,1,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1