Encoding and Decoding Cloud Data

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. 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 a few variables. These are:

  • z
  • i
  • i2
  • i3
  • ☁ data

two lists, named 'users' and 'hold', 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 certain number, such as 00, and assigns each item a (preferably two-digit) number.

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 [☁ data 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 <(i3) > (length of (z))>>//if the letter is not included in the variable "z" the loop will not end, 
            change [i3 v] by (1)
        end
        if <(i3) > (length of (z))> then //so it checks if "i3" is greater than the length of "z"
            set [i3 v] to (length of (z)) // and replaces the letter with a "?" if that is true
        end
        if <(i3) < [10]> then //If i3 is less than 10, it needs to be ex: 03 instead of 3
            set [☁ data v] to (join (☁ data) (join [0] (i3)))
        else
            set [☁ data v] to (join (☁ data) (i3))
        end
        set [☁ data v] to (join (☁ data) [00])
    end
end

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 [1]
set [i2 v] to [2]
delete (all v) of [users v]//This list is the list we are decoding the data into, or the 'cloud list'
delete (all v) 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 [i3 v] to (join (letter (i) of (☁ data)) (letter (i2) of (☁ data))
    change [i v] by (2)
    change [i2 v] by (2)
    if <(i3) = [00]> then
        add (hold) to [users v]
        delete (all v) of [hold v]
    else
        add (letter (i3) of (z)) to [hold v]
    end
end

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

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 you don't feel safe editing the project's JSON.

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]//and an extra ]
set [p1 v] to [1]
repeat (length of (Characters))
  set (letter (p1) of (Characters)) to (join([floor v] of ((p1)/(10)))(round((p1) mod (10))))//edited block creates a variable with the name of a specific character, with that characters 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, 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

See Also

  • This page was last modified on 25 September 2017, at 09:56.