LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Save and load variables?

Root / Programming Questions / [.]

CrazyblobCreated:
Hey, I've tried looking around the forum but couldn't really find this question. How would I be able to save and load variables by saving a file? Let's say i let the user input a variable wert$ and i wanted to load what the user inputed.. how?

You can save/load strings as text files:
SAVE "TXT:filename",string$
string$=LOAD("TXT:filename",FALSE) 'false disables the message box when loading files.

Thank you!

What about for a value instead of a string? (1,2,3)

For that you have to use an array. For example, to save/load the variables A, B, C:
'saving
DIM ARRAY[3]
ARRAY[0]=A
ARRAY[1]=B
ARRAY[2]=C
SAVE "DAT:filename",ARRAY

'loading
DIM ARRAY[0] '(if you haven't created the array already) (Also, LOAD automatically expands/shrinks the array to the the correct size)
LOAD "DAT:filename",ARRAY,FALSE
A=ARRAY[0]
B=ARRAY[1]
C=ARRAY[2]

Sorry but I don't really get it. I want to save the current state of a variable but it says I can't have a duplicate when I rename the arrary. What's the letters (a,b,c) for? I'm probably being really dumb, sorry

What if the user inputs the amount they want in the variable? How would I array that

DAT files are strictly only for numbers, it saves an array of numbers into a file. Then you can open that file later, and get all the values back. A,B, and C are your variables, you can save as many as you want, 12Me21 was giving an example of saving 3 variables named A,B, and C. So to elaborate on his answer, at any given time in your program, you can run the save code, and at any given time you can load that file. If you're going to store strings it gets a little more complicated
'A function that saves my current game variables
DEF SAVEMYGAME
 DIM A[0]
 PUSH A,MY1STVARIABLE
 PUSH A,MY2NDVARIABLE
 PUSH A,MY3RDVARIABLE
 PUSH A,MY4THVARIABLE
 PUSH A,MY5THVARIABLE
 SAVE "DAT:MYSAVEFILE",A
END

'A function that loads the game variables, setting all of their values
DEF LOADMYGAME
 IF CHKFILE("DAT:MYSAVEFILE") THEN
  DIM A[0]
  LOAD "DAT:MYSAVEFILE",A,0
  MY1STVARIABLE=SHIFT(A)
  MY2NDVARIABLE=SHIFT(A)
  MY3RDVARIABLE=SHIFT(A)
  MY4THVARIABLE=SHIFT(A)
  MY5THVARIABLE=SHIFT(A)
 ENDIF
END
So a test program would be something like this
MY1STVARIABLE=1234
MY2NDVARIABLE=1.234
MY3RDVARIABLE=4321
MY4THVARIABLE=43.21
MY5THVARIABLE=432.1
SAVEMYGAME 'Save these values to a file
Now say, you're opening up the game again and want to restore those values
MY1STVARIABLE=0
MY2NDVARIABLE=0
MY3RDVARIABLE=0
MY4THVARIABLE=0
MY5THVARIABLE=0

LOADMYGAME
'Now:
'MY1STVARIABLE is equal to 1234
'MY2NDVARIABLE is equal to 1.234
'MY3RDVARIABLE is equal to 4321
'MY4THVARIABLE is equal to 43.21
'MY5THVARIABLE is equal to 432.1
Does that help? It's different for strings, for strings you'll want to use a TXT file instead of DAT file type.

Alright thank you. Yeah that helped a lot. I managed to save and load the variables but there was a issue, when I tried to print the variable, it would always be 0. (followed 13Me21 example.) I guess I did something wrong, I fixed the issue with
if c==array[2] then c=2
Am I doing something wrong?

How this just does NOT want to work