? DATA, READ, and RESTORE ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password
What's going on?? See: Site Update March 2022

DATA, READ, and RESTORE

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.
Author
PizzaHacker
Updated
Rating
5 votes
Categories
Keywords
  • data
  • array
  • restore
  • read
10 Comment(s) rando rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website Wonder if I could make a 3d game with a 3 dimensional array... jkjk I know I need an engine for 3d rando rando Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Avatar Taboo I didn't change my avatar for 180 days Website Now that I think about it, I can store data with one for the map. Midnoclose Midnoclose Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website I keep getting an Out of DATA error. Ptcguy Ptcguy First Week Joined in the very first week of SmileBASIC Source Website Minecraft Is Awesome! I love Minecraft! Express Yourself Beginner Programmer I'm just starting out! I'm still trying to grasp the basics. Programming Strength Is there a way to enlarge arrays? snail_ snail_ Power User QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! Helper Received for being very helpful around SmileBASIC Source Achievements Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements 2D arrays? No. 1D arrays, you can PUSH empty entries to the top or SHIFT empty entries to the bottom. 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself you can enlarge arrays with = or COPY PixelStudio PixelStudio Oh also what about words like"hello" PixelStudio PixelStudio Would two dimensional arrays work as a horizontal side scrolled on a 2d game SquareFingers SquareFingers 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. Super_Defaultio Super_Defaultio Video Games I like to play video games! Hobbies Day Person I like the warm sunshine and wake up early! Express Yourself Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself I like this tutorial, but what would make it better if you added the syntax for the commands.