Saving and loading high score

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: “high_score.txt”, playdate.file.kFileRead) - to open the file for reading

and “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: “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 returns a file object, and it’s that object that you can read and write from.

So to write, you can do:

local scoreFile ="high_score.txt", playdate.file.kFileWrite)

And to read:

local scoreFile ="high_score.txt", playdate.file.kFileRead)
hs = tonumber(scoreFile:readline())

Works great, thanks, and now I know how to use tostring and tonumber


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 = {

-- save highscores
playdate.datastore.write( highscores, "highscore" )

-- load highscores
highscores = "highscore" )