Save/load using datastore in simulator

I'm struggling to get my save system to work in the simulator.

I'm using this at the end of level:

playdate.datastore.write(levels, "save", true)

Which works as desired with the file being created in the SDK folder.

Then I added this to the game launch:

levels = playdate.datastore.read("save")

And it worked perfectly!

My problem is that once I deleted the save.json file from the SDK folder to start a fresh save file my game crashed on loading. I guess because my "levels" table is being created and initialised but then emptied by the datastore.read.

So I tried to make it only read the save.json if the file exists:

if playdate.file.exists("save") then
        levels = playdate.datastore.read("save") 
end

Now it won't read the file even though it exists...

Am I using file.exists incorrectly?

Don't worry, I've figured it out:

if playdate.file.exists("save.json") then
       levels = playdate.datastore.read("save")
end

playdate.file.exists requires the .json file extension, whereas with read and write it should be omitted.

5 Likes