Save and load variables?
Root / Programming Questions / [.]
CrazyblobCreated:
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]
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 ENDSo 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 fileNow 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.1Does 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=2Am I doing something wrong?