(Redirected from Encoding and Decoding)

(☁ var) This article or section uses Cloud Data. Users who are New Scratchers or are using the Offline Editor cannot make projects using Cloud Data or use it in other users' projects. To use Cloud Data, the Scratcher status is needed and the Online Editor has to be used.

Encoding a cloud variable is the process where one takes a list or string and compiles it into a number-only format. The reasoning for this is that Scratch currently only supports numbers in a cloud variable due to their storage limitations. Numbers take up less space and are easier to process in the Scratch database. This can be used to create large online leader boards or even massive online multiplayer games.

Quick Method

Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (November 2023)
Reason: Encoding & decoding scripts need further elaboration for clarity

Variables/Lists Needed

Variable names are kept short for faster script execution. The variables assumed by this method are:

(z) //Stores an string alphanumeric string of accepted characters to be encoded/decoded
(i) //The following ‘i’ variables are used as counters within script execution loops
(☁ data)

Also, create the following list:



For example, if the first letter of the encoding process is "b", the code representing "b" would be 12 because the twelfth letter of the variable "z" is "b". The variable that contains all the letters used for parsing is named "z" because each time the variable's value is called, a shorter name takes up less processing power to report the specified character.

when gf clicked
delete all of [key v]
set [z v] to [0123456789abcdefghijklmnopqrstuvwxyz-_]
set [i3 v] to (1)
repeat (length of (z))
add (letter (i3) of (z)) to [key v]
change [i3 v] by (1)
set [encoded v] to ()
set [decoded v] to ()

define encode(data)
set [encoded v] to ()
set [i v] to (1)
repeat(length of(data))
if <(item # of (letter (i) of (data)) in [key v])<(10)> then
set [encoded v] to (join (encoded)(join(0)(item # of (letter (i) of (data)) in [key v])))
set [encoded v] to (join (encoded)(item # of (letter (i) of (data)) in [key v]))
change [i v] by (1)
set [☁ data v] to (encoded)


Decoding takes the encoded number data and compiles a list out of it. Modifications must be made with the encoder and decoder if multiple lists need to be compiled for a project's particular purposes.

define decode (data)
set [decoded v] to ()
set [i2 v] to (1)
repeat ((length of (data))/(2))
set [decoded v] to (join(decoded)(item (join (letter (i2) of (data))(letter ((i2) + (1)) of  (data))) of [key v])
change [i2 v] by (2)

In-Depth Method

Archive.png This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.

Note Warning: This method requires a few edited blocks which can be obtained by editing the JSON of a Scratch project, so if done wrong it can render the project unopenable. Use the above method or make a backup of the project if one does not feel safe editing the project's JSON. This method will only work in Scratch 2.0.

Variables/Lists Needed

This method uses a script to create most the variables required, it should look something like this:

define Create Vars
set [characters v] to [� !"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~0123456789]
set [p1 v] to [1] //This variable is the ID that will be given to the character
repeat (length of (characters))
  set (letter (p1) of (characters)) to (join([floor v] of ((p1)/(10)))(round((p1) mod (10))))//This edited block creates a variable with the name of a specific character, with that character's 2 digit number ID inside the variable
  change [p1 v] by (1)

Additional variables that must be manually created include:

(☁ data)

Lists include:



The encoding process works in the same way as the above method, although this method uses a faster way of encoding characters to their 2 digit ID number that includes another edited block:

define Encode
delete all of [bypass v]
set [p1 v] to [1]
repeat (length of [cloudlist v])
  set [p2 v] to [1]
  set [p3 v] to (item (p1) of [cloudlist v])//Sets a variable to the item of the cloud list we are currently encoding
  repeat (length of (p3))
    set [p4 v] to (join ((letter (p2) of (p3)) of [Sprite1 v])[1])//This edited block is used to pull the data out of those variables set with "Create Vars" replace Sprite1 with the sprite that ran the Create Vars custom block
    add (letter (1) of (p4)) to [bypass v]
    add (letter (2) of (p4)) to [bypass v]//Adds the 2 digit ID of the character to the list
    change [p2 v] by (1)//Changes to the next character in the item of the cloud list so it can encode it
  add [0] to [bypass v]
  add [0] to [bypass v]//These 0s tell the decoder to make a new item
  change [p1 v] by (1)//Changes to the next item in the cloud list so it can begin encoding
set [☁ data v] to (bypass)//The bypass list is used to bypass the 10240 character limit of the join block, it also prevents cloud flooding (setting a cloud variable more than 3 times a second)


The decoding script for this method will look very similar to the script for the easier version. There are no edited blocks in this script.

define Decode
set [p1 v] to [1]
set [p2 v] to [2]
delete all of [cloudlist v]
delete all of [hold v] //This list collects the data for one item of the cloud list, it is then added to the cloud list and the process is repeated
repeat ((length of (☁ data)) / (2))//Since we encoded each character into a 2 digit number, they must be decoded in sets of 2 digits
    set [p3 v] to (join (letter (p1) of (☁ data)) (letter (p2) of (☁ data)))//Uses two variables (p1 and p2) for speed, since using another variable is faster than using (p1+1)
    change [p1 v] by (2)
    change [p2 v] by (2)
    if <(p3) = [0]> then//Does not need to be 00, because according to scratch, 00 = 0 even when they are read as strings
        add (hold) to [cloudlist v]//Adds decoded data to the cloud list in one item
        delete all of [hold v]//Deletes and repeats
        add (letter (p3) of (characters)) to [hold v]//collects decoded data

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.