Saving and Loading Game Data Text String$

Previous topic - Next topic

Widget101

Hi,

I'm trying to understand the best way to save and load data for my current game project which so far has just over 15600 variables in vars, dims and string$.

I think I'm ok with dims and the standard variables but am after a bit of advice for strings.

I've gone through the GLBasic help example but for some reason this comments out the key bit I'm after (// READSTR 1, x2$, LEN(xx$)).

I've managed to get it to work by saving the length of the string into the save file before saving the actual $tring as I can't otherwise see how it knows how long it has to be if its not already loaded it. I'm wondering if this is the best way to do it or whether there is a better solution. With this way every string in the save file is going to need its length saved before it so it can load the length into a variable then use that variable in the READSTR command to set the length for the text to follow it.

My simplified test code is below:
Code (glbasic) Select

SETSCREEN 1024, 768, 1
SETFONT 0

//Set up some random variables
GLOBAL test$
GLOBAL var1
GLOBAL var2
GLOBAL var3
GLOBAL b$
GLOBAL var4
GLOBAL test[]
DIM test[5]

//populate the variables
test$ = "savefile.txt"
var1=57
var2=103
var3=999
b$="This Text String Could Be Any Length!!!!"
var4=345
test[0]=501;test[1]=602;test[2]=703;test[3]=804;test[4]=905

//create the save file
OPENFILE(1, test$, FALSE) //false=write mode
WRITEIEEE 1,var1
WRITEIEEE 1,var2
WRITEIEEE 1,var3
WRITEIEEE 1,LEN (b$) //saving the $tring length - is this required??
WRITESTR  1,b$
WRITEIEEE 1,var4
FOR f=0 TO 4; WRITEIEEE 1,test[f]; NEXT
CLOSEFILE 1

//Clear variables
var1=0
var2=0
var3=0
b$=""
var4=0
FOR f=0 TO 4; test[f]=0;NEXT

//See if they reload
//open the save file and populate the variables from it
LOCAL textlength
OPENFILE(1, test$, TRUE) //true=read mode
READIEEE 1, var1
READIEEE 1, var2
READIEEE 1, var3
READIEEE 1,textlength //load the length of the following text string
READSTR 1,b$,textlength //load the text string
READIEEE 1, var4
FOR f=0 TO 4; READIEEE 1,test[f]; NEXT
CLOSEFILE 1

//print the loaded variables to the screen
PRINT var1,0,0
PRINT var2,0,20
PRINT var3,0,40
PRINT b$,0,80
PRINT var4,0,120
FOR f=0 TO 4; PRINT test[f],0,140+f*20; NEXT

SHOWSCREEN

MOUSEWAIT


Any help would be appreciated.  :)

Kitty Hello

An easy way would be writeline for each variable. Otherwise you write a binary file. So first must be a file format nilumber to read older versions..

Widget101

Thank you!

That is much better - doesn't need to separately worry about the length of the text.

Code (glbasic) Select

c$="A random line of text!"
WRITELINE 1,c$ //to write the text line to the file
READLINE 1,c$ //to read the text from the file back into the variable


Many thanks.