(☁ var) This article or section is about or uses Cloud Data. Users with the New Scratcher status or ones who are using the Offline Editor cannot make projects using Cloud Data or use it in other users' projects. New Scratchers need to have Scratcher status. Offline editor users need to use the online editor.

Encoding a cloud variable is the process where one takes a list, or a string, and compiles it into a number-only format in a cloud variable. The reason for this, is because 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 data base. It can be used for creating large online leader-boards or even massive multiplayer online games.

Quick Method

Variables/Lists Needed

This method will assume several variables. These are:

(z)
(i)
(i2)
(i3)
(☁ data)
(encoded)
(decoded)

Also, create the following lists:

(key::list)

Encoding

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-_] //the variable name is kept short for faster script execution
set [i3 v] to (1)
repeat (length of (z))
add (letter (i3) of (z)) to [key v]
change [i3 v] by (1)
end
set [encoded v] to ()
set [decoded v] to ()

define encode(data)
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])))
else
set [encoded v] to (join (encoded)(item # of (letter (i) of (data)) in [key v]))
end
change [i v] by (1)
end
set [☁ data v] to (encoded)

Decoding

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 [i2 v] to (1)
repeat ((length of (data))/(2))
set [decoded v] to (join(decoded)(item # of (join (letter (i2) of (data))(letter ((i2) + (1)) of (data))) in [key v])
change [i2 v] by (2)

In-Depth 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. (July 2017)
Reason: Script comments do not provide sufficient explanation
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:

(characters)
(p1)
(p2)
(p3)
(p4)
(☁ data)

Lists include:

(cloudlist::list)
(bypass::list)
(hold::list)

Encoding

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
  end
  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 encode it
end
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)

Decoding

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
    else
        add (letter (p3) of (characters)) to [hold v]//collects decoded data
    end
end

See Also

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