LoginLogin

DATA, READ, and RESTORE

Root / Submissions / [.]

PizzaHackerCreated:

Description

DIM is a function that declares an array and its dimensions. You can use up to 4 dimensions. READ is a function that reads the data from DATA. By default, it starts reading from the beginning of the first DATA declaration. DATA is a function that stores any kind of data (numbers and strings/characters) for it to be read. You can organize the data for better reading if you're using 2 dimensions or more (See Example #2). RESTORE is a useful function for data reading. Instead of allowing READ to read data starting from the beginning, you can make it start directly from the label. You can use a FOR loop to read all the data and store it in an array for future implementation.

Example #1 - One-Dimensional Array

CLS
A=10 'Size of the aray
DIM ARRAY[A] 'Declaring the array and its size
RESTORE @TESTDATA 'Points where to start reading the data
FOR I=0 TO A-1 'From 0 to 9 = 10 iterations for each data
 READ C 'With READ you need to store all the data in a temporary variable, in this case C
 ARRAY[I]=C 'Now store each data declaration to the array
NEXT

FOR I=0 TO A-1 'This FOR loop is for printing the data
 PRINT STR$(ARRAY[I]) 'This converts the data to string
NEXT
END

@TESTDATA 'DATA label
DATA 4,9,2,16,86,12,5,129,56,0 'You can use a comma to separate DATA or use a new DATA line for each one.

Example #2 - Two-Dimensional Array

CLS
A=3 'Number of rows
B=2 'Number of columns
DIM ARRAY[A,B] 'The first one defines the # of rows, the second one the # of columns
RESTORE @TESTDATA

FOR I=0 TO A-1
 FOR J=0 TO B-1
  READ C
  ARRAY[I,J]=C 'The first read is in position 0,0 then 0,1 inside the array. After that it goes into the first loop again and continues with the next row.
 NEXT
NEXT

FOR I=0 TO A-1
 FOR J=0 TO B-1
  PRINT STR$(ARRAY[I,J]);" "; 'I put a blank space to separate each column
 NEXT
 PRINT 'I put this empty PRINT to create a new line for each row.
NEXT
END

@TESTDATA
DATA 1, 2
DATA 3, 4
DATA 5, 6
Two-dimensional arrays are really useful when programming 2D games, but it has other uses too. Source I used the information from this GameFAQs PetitComputer answer http://www.gamefaqs.com/ds/663843-petit-computer/answers/331284-using-data-read-and-restore and updated it for SmileBASIC and added my own commentary.

I like this tutorial, but what would make it better if you added the syntax for the commands.

READ C 'With READ you need to store all the data in a temporary variable, in this case C Not true. READ ARRAY will work just fine. PRINT STR$(ARRAY) 'This converts the data to string PRINT does not require the data to be converted to string, using STR$ here is a needless complication.

Would two dimensional arrays work as a horizontal side scrolled on a 2d game

Oh also what about words like"hello"

Is there a way to enlarge arrays?

Replying to:Ptcguy
Is there a way to enlarge arrays?
2D arrays? No. 1D arrays, you can PUSH empty entries to the top or SHIFT empty entries to the bottom.

Replying to:Ptcguy
Is there a way to enlarge arrays?
you can enlarge arrays with = or COPY

I keep getting an Out of DATA error.

Wonder if I could make a 3d game with a 3 dimensional array... jkjk I know I need an engine for 3d

Replying to:rando
Wonder if I could make a 3d game with a 3 dimensional array... jkjk I know I need an engine for 3d
Now that I think about it, I can store data with one for the map.