My Save Code Format


Over many years of developing Scratch projects, I have settled on a simple-to-use and easy-to-understand save code format and now use it on pretty much everything. I call it CSKV, which loosely stands for comma-separated key/values. The only reserved characters are colon : and comma ,. I usually don't need escape characters so my simplest implementation does not use them but it can be modified to be accounted for.

Example (with colour showing the key and values):

user:awesome-llama,id:8832510,scratchteam:false

But what happens if you need to store more than a single value? Lists may be stored in each key. Implementation depends on the project but the general one is to have the list items separated by commas with the first item representing the length of the list:

recent_projects:3,The Mast,Submersible,Road Network Designer

The code responsible for reading the save code shall be designed to understand what data is expected for a given key. For example, recent_projects will be expected to be a list, the length determined by the first item. user will be expected to always be a single value. Keys are always 1 item and in a save code a colon will always follow it.

I often include a magic number at the start of the save code, like this:

account_info,user:awesome-llama,id:8832510,scratchteam:false

This helps to indicate the purpose or source of the data and provide simple validation if user-inputted.

Also, with save codes that are intended to be used by others and not just something internally-used, I ensure a project version number is provided as a key, along with optional username and date the code was generated. The following example is from my game Submersible (v for project version, sv for save code version) and time (t, seconds since 2000):

SUB,v:1.0,sv:1,t:731066503,[...]

The format is deliberately structured so it can be represented in a single list in Scratch (although not required). A decoder may split the save code by commas and colons into list items. If a colon is found, the item it found before it is guaranteed to be a key. A decoder may prepend an exclamation mark ! onto the key to help differentiate it from the values (note this becomes another reserved character). This is useful if reading the list with the "item # of [] in list" block as the exclamation mark helps ensure it will only find keys.


Here is a Scratch project with the scripts I typically use to implement it: https://scratch.mit.edu/projects/114633511/


If using this for a save code that saves game progress, it is apparent that the simplicity and readability also means it is easy to cheat. I suggest if you want to stop the simplest, most opportunistic cheating, you include a checksum for the important values (e.g. currency, high score). Just remember the "see inside" button exists on all Scratch projects and provides many more opportunities to cheat.


A list of notable projects that use this system: