From Test-Scratch-Wiki

Translate: - English - Türkçe 

The act of saving data is to record a variable or a list in a Scratch project so it can be read, or used at a later time.

Saving Data in Lists

With the introduction of lists in Scratch 1.3, it is now easier to create projects that incorporate this function. When you right-click a list it gives options to import or export the list. This creates a .txt file of the list in the desired location.

Pros

The ability to save your progress in a game is an extremely useful tool for game developers. Some of the benefits are:

  • Ability to leave a game and pick up at the same point as you left off.
  • Ability to create your own game files.
  • Creators can more easily modify and upgrade their projects.
  • Players can customize certain aspects of the game.

Cons

Although saving data from the game can be very useful, there are also some problems:

  • If the scripts are not well written, the project will not perform correctly.
  • Players can easily edit the .txt file to "hack" the game.
  • Players can customize certain aspects of the game not meant to be customized.
  • Players that are not knowledgeable in Scratch might get easily confused.

How to Prevent Problems

There are several ways to try and prevent the above cons from happening:

  1. Test your save scripts multiple times. Ensure they work under all circumstances.
  2. Make sure that users download your project and not play it online. This can be done with the obsolete block.
  3. You can attempt to encrypt or code your list so that only the computer can decipher your saved data. This, however, is extremely time consuming as well as difficult to do.
  4. Try not to label the items in your save list; labeling them makes it easier for hackers to correctly edit their saves.
  5. You can use a mathematical checker to only allow logical data.
  6. Include detailed instructions on how the game works for new players so that they can understand exactly how to play it.

Examples

Farmville V1.5 by dazman

Shaper by jerry1000

Tutorial

This tutorial will go over making a simple save system which stores values in a list.

All the data will be stored in a single string which will be put back into the project at a later date to load the old data the player had.

To start, make a list called "Save Data". Next, make the following custom block which will save data:

define save (value)
add (join (value) [;]) to [Save Data v]

You can then make a script to save any variables you want:

save (x position) //category=custom
save (health) //category=custom
save (money) //category=custom

The next step is to give the save code to the user so they can enter it in later. First you want to make a variable called code. You can do so like this:

set [code v] to [] //Make sure there is nothing inside the second input
repeat until <(length of [Save Data v]) = [0]>
set [code v] to (join (code) (item (1 v) of [Save Data v]))
delete (1 v) of [Save Data v]
end
add (code) to [Save Data v]
show list [Save Data v]

The user can then export the list or copy and paste the code somewhere.

Finally, the user needs to be able to load data. Make a list called "Loaded Data" and a variable called "i", then make the following custom block:

define load data (data)
set [i v] to [1]
set [value v] to [] //Make sure the second input is blank
repeat until <(i) > (length of (data))>
repeat until <(letter (i) of (data)) = [;]>
set [value v] to (join (value) (letter (i) of (data)))
change [i v] by (1)
end
add (value) to [Loaded Data v]
change [i v] by (1)
set [value v] to [] //Make sure the second input is blank
end

Now you simply need to load the data in the order that you saved it. You first ask the player for their save code, then load the data, and then store the loaded data. So for the above example where "x position" is stored, then "health", then "money". The loading script would look like this:

ask [Save code?] and wait
load data (answer) //category=custom
set x to (item (1 v) of [Loaded Data v])
set [health v] to (item (2 v) of [Loaded Data v])
set [money v] to (item (3 v) of [Loaded Data v])

If you get lost you can look here for help: https://scratch.mit.edu/projects/56698396/

Saving Data with Encryption

Another method for saving data in Scratch is through the use of . With this method, the user is prompted to enter a code when the game starts and is given a new code when he or she wishes to leave. These codes can be used to determine where in the game players left off. For example, let's say that you were playing a scrolling game and you had 2 lives left and 5 cash. When you saved the game, you would get a code of 25. When you resumed the game, it would set your lives to the first letter of the code and set your cash to the second letter.

Pros

This method of saving your game can be just as effective, if not more effective than the first method listed. Here are some benefits of this method as compared to it:

  • It works online and offline.
  • Codes can be used on any computer.
  • The scripts are simpler.

Cons

Although this method is a great way to save games, it does have quite a few cons:

  • The player has to remember or write down the code. This can be very annoying for the player as some advanced games could involve codes with as many as 20-30 characters!
  • Unless you have great experience with encrypting and decrypting files, your codes may be very easy to crack. This can allow players to modify their saves with ease.
  • Players may share "cracked" codes with other players, allowing others to cheat on your game.
  • This method is difficult to use in situations involving numbers with a wide range. A player's money could reach heights of millions, so programming a script that would understand how to deal with any value for these numbers could be very tedious and time consuming.

How to Prevent Problems

  1. Try to make codes as short as possible or show the code in a list so users can copy and paste the code.
  2. Leave instructions on how to use the code in the project notes.
  3. Encrypt your codes.
  4. Have all valid codes fall under a single mathematical equation and all codes checked with it - some false codes may go against the equation and will therefore not be accepted.
  5. Make sure that your scripts will work for very long codes with a range of values.

Example

SAvegame test by kwickgamer

Saving Data in Variables

Another method to save data is through variables. When you save your project, all the current variable values are saved. This is one of the most popular methods as it is easy and straight forward. It is easy for the player to save their progress. The project creator although, has to create a way to initiate the saved variables when the game starts.

Pros

This method is most commonly used because:

  • It is quick and easy for the player.
  • It is easy for the Scratcher to do, even with little knowledge.

Cons

Despite how easy it might seem, this method has a con: It cannot be used on online project players.

How to Prevent Problems

  1. Try to "hide" your variables so people will not try and change them
  2. Use a mathematical checker so that people do not change the variables.
  3. Make sure that your project calls on the variables before the game starts, so that the data is properly restored.
  4. Test the project to ensure that this method works with your project and the code works to restore a saved game.

Example

SynOS 7: Purple Crystal by 08jackt

Saving Data in the Cloud

Main article: Cloud Data
See also: Global High Scores

Cloud data is stored in the "cloud", or on the Scratch server, meaning it is stored on the web. Cloud data does not reset when you view a project; it gets globally updated by anyone changing the cloud variable. For example, if User A views a project and changes the variable from "1" to "2", when User B views the project, the variable will stay as "2". The variables globally update, with the speed of typically 2 seconds with a Firewall on to update the value. Storing data on the cloud is difficult, as Scratch 2.0 has disabled strings to be saved on cloud temporarily.

This is typically accomplished by creating two cloud lists. The first is a list of users who have stored data. The second is a list with the saved data, corresponding to the users in list number 1.

Pros

  • No need to remember or write down any codes or encrypted text.
  • Data can be loaded back after clicking the green flag.
  • Data can be stored separately for each individual Scratcher

Cons

  • If cloud data were to be lost in any way, all saves would be gone.
  • There is a limit of how many people can use it
  • Cloud data can be confusing to use

How to Prevent Problems

  1. Test out the cloud data system, before publishing a project with cloud saving in it.
  2. Include a back-up variable to store information on.