Saving game data

BlitzMax Forums/BlitzMax Beginners Area/Saving game data

Gary Leeds(Posted 2010) [#1]
I am looking to save several variables while my program is running and as far as I can see writestream is the correct way but there does not seem to be an obvious way of reading back the data to the variables

Basically I want to do something similar to
out=WriteStream("mygame.ini")

if not out RuntimeError "Failed to open a WriteStream to file mygame.ini"

WriteLine out, score
WriteLine out, level
WriteLine out, position[0]
WriteLine out,position[1]
WriteLine out,position[2]
WriteLine out,position[3]

CloseStream out


but what is the correct way to read these back into the variables on reload?

Also, if the file already exists will it throw an error and fail or will it overwrite the file (which is what I am hoping for)

Regards
Gary


degac(Posted 2010) [#2]
You can use FileType() to check if the file already exists or not.
With WriteFile() you overwrite the file and its content.

To read back values (assuming you know the 'right order' of the datas) you could do something like

local in:tstream=ReadFile("myfile.ini")
if in

score=Int(ReadLine(in))
level=Int(ReadLine(in))
position[]=Int(ReadLine(in))
closefile IN
else
print "File doesn't exist"
end if


Assuming that SCORE, LEVEL and POSITION are INT


Volker(Posted 2010) [#3]
You can use a mod like an inifile handler, muttley has made a good one.
Although you could look at Bruceys TPersist mod, with which
you can store complete objects as XML on disk.


Gary Leeds(Posted 2010) [#4]
cheers, reading the manual it didnt look like it could be read back as anything but strings

thats spot on, thanks


slenkar(Posted 2010) [#5]
yeah dont forget to check out bruceys persistence mod


Czar Flavius(Posted 2010) [#6]
I can also suggest you use XML files and use Brucey's libxml module. XML is very easy to modify and read and plus the data files can be edited by hand too. You also DON'T need to worry about the order of variables - it won't come crashing down because you read one attribute before the other.