Getting Started with Datastore and Files

As someone who is self-teaching everything to do with LUA and coding, I am admittedly stumped by some of the more basic elements.

I've done some scavenging of other forum posts to get some general ideas and contexts to try to help me understand the documentation in the SDK. It left me with more questions, so I would like to solicit some tips, resources, and answers surrounding datastore, files, and "saves". This could potentially be a mini resource for others new to coding like myself.

General Items:

  • Do you have any tips for handling game saves that you wished you learned earlier?
  • Are there any pitfalls new developers should look out for?
  • Do you have some resources that you find very helpful when learning or working with saves?

The Basics:
Please correct me if I get something wrong.

A "game state" is just a series of data points that are being tracked and manipulated as needed to facilitate things that are happening/have happened on the screen.

When it comes time to save your progress, you can do this via the datastore functions. Essentially this is taking all those vital data points of your game state and storing them in a table and that gets written to a JSON file.

Hopefully I have that right. We will find out in the comments! XD
Now we get to the limit of my knowledge and have the starting questions.

Starting Questions:

  1. How Does Resuming a Save Work?
    I read that you use playdate.file.open(path, playdate.file.kFileWrite) to read a file and open it for writing. What is actually happening when you read a file? Is that loading the data of the table into the game memory, thusly setting variables to be used by your other game functions? Or are there other actions you need to observe?

  2. How Do You Re-Save?
    I see playdate.datastore.write(table, [filename], and playdate.file.file:write(string). Am I correct in assuming that the former is only for creating new "saves" and the latter is what you would use for making updates to a "save"? Or are you supposed to clear the table in a file and write the fresh data?

  3. Datastore Delete VS. File Delete
    These look to be functionally the same, but I'm sure they are not just because I know there is nuance that I don't understand yet. Can someone help explain the function of these two deletes?

3 Likes