Save and Load Project Data
-
Introduction
When we save a project, all the data stored in the variables/lists/tables are saved as well. However, when users run projects they don’t own, they can not save data this way.
For example, suppose you have published a game project, and you want other users to be able to save their “progress”, so that they can resume the game in the future.
One solution is to use “project data”, which allows the program to save the data of any user as part of the project.
Save Project Data
To save some data as part of a project, you can use the following block:
It takes 3 inputs:- scope: If this data is “public”, then all users can read this data when they run this project. If this data is “private”, then other users won’t be able to read this data, so it is private to the user who saves this data under his/her user ID. This block will not do anything if the user has not logged in, since it needs to know the user’s ID to decide how to save this data.
- data: the actual data to be saved, which can be any text.
- name: the name of this data, which is needed to read this data out later.
For example, if your game has many levels, and you want to save each user’s current level (stored in the variable “my level”), you can save it as a “private” data for each user:
Load Project Data
After the data has been saved as part of the project, you can use the following block to read it out at any time:
You need to specify the name associated with that data to read it out.Each user has permission to read out any data that is saved by that user, either it is public or private. However, if the data was saved as “private”, then only that user can read it later.
For example, suppose user A has saved 2 pieces of data: a public data named “p1”, and a private data named “p2”.
Later, user A can load both data items out using the names “p1” and “p2”. However, for another user B, he or she can only read the data named “p1”.
List All Data Names
Suppose you save each user’s game score using the “user name” block (from the sensing category) as the name, and you would like to list all the scores of all users. You can use the following block to get the list of all names:
The result will be a long text separated by “&&”, like “x&&y”. This is assuming the data names do not contain “&&” themselves, so you can easily split the data names into a list using the following block: