bkumanchik
(Brian Kumanchik)
#1
I am trying to save and load my games high score (hs) and can’t quite figure out how to do it using the Filesystem method (not JSON)
I am using:
playdate.file.open( “high_score.txt”, playdate.file.kFileRead) - to open the file for reading
and
playdate.file.open( “high_score.txt”, playdate.file.kFileWrite) - to open the file for writing
The file is created as expected but I can’t figure out the syntax to actually write my variable (hs) to the file, I’ve tried:
playdate.file.open( “high_score.txt”, playdate.file.file:write ( hs ))
I will also need to read from the file and store it in a variable (hs) and I’m also having trouble with that. are there any examples available
I’m pretty sure I just don’t understand the syntax for how to use these functions. Any help would be appreciated, thanks
Brian
playdate.file.open
returns a file object, and it’s that object that you can read and write from.
So to write, you can do:
local scoreFile = playdate.file.open("high_score.txt", playdate.file.kFileWrite)
scoreFile:write(tostring(hs))
scoreFile:close()
And to read:
local scoreFile = playdate.file.open("high_score.txt", playdate.file.kFileRead)
hs = tonumber(scoreFile:readline())
scoreFile:close()
bkumanchik
(Brian Kumanchik)
#3
Works great, thanks, and now I know how to use tostring and tonumber
Brian
Nic
(Nic Magnier)
#4
For stuff like saving highscores or game progress I would also suggest to look into datastore which allow you to save/load lua tables very easily
highscores = {
main,
daily,
hard
}
-- save highscores
playdate.datastore.write( highscores, "highscore" )
-- load highscores
highscores = playdate.datastore.read( "highscore" )
3 Likes