(☁ 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 converts it into a number-only format because cloud variables only accept numbers. Decoding is the process in reverse. This can be used to create online leader boards or even multiplayer games.
Quick Method
The method shown here represents characters as 2-digit numbers. For example, the character "b" maps to 12. It is case-insensitive, so "B" also maps to 12. Numbers smaller than 10 are padded with a zero, so the character "5" maps to 05.
Setup
A list is needed to store all the characters that can be encoded and act as a lookup table to find the number associated with character and vice versa.
(characters::list)
Each list item should be 1 character. The following code can be used to create the list's contents:
when gf clicked delete all of [characters v] set [character set input v] to [0123456789abcdefghijklmnopqrstuvwxyz-_ ] // characters to put in the list set [i v] to [1] // counter repeat (length of (character set input)) // enumerate over each character of the input string add (letter (i) of (character set input)) to [characters v] // add character to list change [i v] by (1) // increment counter end
Encoding
To encode to the numerical representation, enumerate over the characters of the input string. Each character is converted to a 2-digit number and joined to the end of the encoded output.
encode [hello123]
set [☁ data v] to (encoded)
define encode (string)
set [encoded v] to [] // the encoded text will be stored in this variable
set [i v] to [1] // counter
repeat (length of (string))
set [code v] to (item # of (letter (i) of (string)) in [characters v]) // lookup the character's number
if <(length of (code)) = [1]> then
set [code v] to (join [0] (code)) // pad with zero
end
set [encoded v] to (join (encoded) (code)) // join onto output
change [i v] by (1) // increment counter
end
Decoding
Decoding involves enumerating over the pairs of numbers and finding the character associated with that code. The character is joined onto the end of an output variable.
decode (☁ data) define decode (data) set [decoded v] to [] // the decoded text will be stored in this variable set [i v] to [1] repeat ((length of (data)) / (2)) // enumerate over pairs of numbers set [decoded v] to (join (decoded) (item (join (letter (i) of (data)) (letter ((i) + (1)) of (data))) of [characters v])) // find the character and join it onto the end of the output change [i v] by (2) // count by 2 because the numbers are in pairs end
In-Depth Method
| This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |
| 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 begin encoding
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