LoginLogin

PRINT Variable error

Root / Programming Questions / [.]

ChaseCZCreated:
Hi, i have problem with printing a variable, actually i can't do almost anything with the variable As you can see on the image on 215th line, there is written "PRINT SAV[2]" SAV is a variable, but once the program gets to this line it says "Subscript out of range" here on the 7th line you can see that there is a variable called SAV[5], so there shouldn't be any problem... here you can see i tried printing the variable before the LOAD command and it worked... it printed the variable without any problems! then i tried printing it after the LOAD command
LOAD "DAT:SAVE",SAV,FALSE
PRINT SAV[2]
and it said that there is the "Subscript out of range" error again. Why does this happen and how can i fix it?

Just answered it on Miiverse, but just to have the answer here as well:
LOAD automatically resizes the array to the size of the file. Your save file seems to be less than three values long, so it shrinks and SAV[2] is no longer valid.
As for fixing it, I guess a simple way you might do it is something like this:
DIM WINS,TUTORIAL,SAV[5],SELECT
'Load file if it exists
IF CHKFILE("DAT:SAVE") THEN
 LOAD"DAT:SAVE",SAV,FALSE
ENDIF
IF LEN(SAV)!=5 THEN
 PRINT "Invalid save file"
 STOP
ENDIF
Something more advanced might try to recognize save files from older versions of the game and convert them; a good way of doing this is to have a version number at the beginning of the save file.

Yes, it finally worked! Thank you a lot. I changed the LOAD command to comment for a while then i saved and then i changed the LOAD command back to normal. I just needed to know that loading the save file resizes the size of the array. Also sorry for bothering on both Miiverse and SBS, i just didn't understand it yet. Yet again thanks!