This article or section is about or uses Cloud Data. Users with the New Scratcher status cannot make projects using Cloud Data or use it in other users' projects. New Scratchers need to have Scratcher status. |
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
- C
One list, named "users", will also be used.
In this example, the project will keep track of users who visit it. However, it is trivial to replace the users list with something else. Alternately, the list and repeat blocks can be removed to only encode a single variable.
Encoding
The encoding process takes each item in a list, separates them with a particular number, and converts each letter into a number with a fixed amount of digits.
This project assumes that it separates them with the code 00
. The code 00
is used in the following script to separate list items because no character in the variable "z" has an indexed position of 0. In simpler terms, when the cloud variable is encoded, each letter of each item of the list is added as a number. The number represents the letter in the variable "z" (which lists all characters) by which the character from the list meets its equivalent position.
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 set [z v] to [0123456789abcdefghijklmnopqrstuvwxyz?] //the variable name is kept short for faster script execution define encode set [i v] to [0] set [C v] to [] repeat (length of [users v]) change [i v] by (1) set [i2 v] to [0] repeat (length of (item (i) of [users v])) change [i2 v] by (1) set [i3 v] to [0] repeat until <<(letter (i2) of (item (i) of [users v])) = (letter (i3) of (z))> or <not <(i3) < (length of (z))>>>//if the letter is not included in the variable "z" the loop will not end, so it stops once it's reached the last letter of z change [i3 v] by (1) end if <(length of (i3)) < (length of (length of (z)))> then //If the number of digits in i3 is less than the maximum possible number of digits, prepend 0s until the length is equal repeat until <not <(length of (i3)) < (length of (length of (z)))>> set [i3 v] to (join [0] (i3)) end end set [C v] to (join (C) (i3)) end repeat (length of (length of (z))) set [C v] to (join (C) [0]) end end set [☁ data v] to (C)
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 set [i v] to [0] set [i3 v] to [] set [C v] to (☁ data)// to avoid getting the value of cloud data too much delete (all v) of [users v] if <not <((length of (C)) mod (length of (length of (z)))) = [0]>> then repeat until <((length of (C)) mod (length of (length of (z)))) = [0]> set [C v] to (join [0] (C)) end end repeat ((length of (C)) / (length of (length of (z)))) set [i2 v] to [] repeat (length of (length of (z))) set [i2 v] to (join (i2) (letter ((i) + (length of (i2))) of (C))) end if <(i2) = [0]> then// 000 = 0 is true, so no matter how long the 0 is it'll be true here add (i3) to [users v] set [i3 v] to [] else set [i3 v] to (join (i3) (letter (i2) of (z))) end change [i v] by (length of (length of (z))) end
In-Depth Method
![]() |
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 |
![]() | 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 you don't 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
- bypass
- hold
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 v) 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 you 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. Luckily, there are no edited blocks in this script!
define Decode set [p1 v] to [1] set [p2 v] to [2] delete (all v) of [CloudList v] delete (all v) of [hold v] //This list collects the data for one item of the Cloudlist, it is then added to the CloudList 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//Doesn't 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 v) of [hold v]//deletes and repeats else add (letter (p3) of (Characters)) to [hold v]//collects decoded data end end