Eng:Global High Scores


 * Although this article focuses on high scores, it contains all the information and encoding processes needed to save user data in a project. For strictly encoding, see this section.

With the new cloud variable feature in Scratch 2.0, global high score lists can be made within a project. These high score lists, stored inside a cloud variable, can take only seconds to update. This tutorial explains how to code a fully functional global high score list, so users' data can be saved for a particular project.

Requirements
Since cloud lists are not released yet, programming this functionality can be quite difficult. Users' data has to be saved in cloud variables, with a limitation: only numbers can be stored. The Scratch Team currently does not allow letters within cloud variables for security reasons. Therefore, scripts are needed to encode and decode the data into sequences of numbers. This tutorial requires the use of:
 * Three lists
 * letter
 * users
 * scores


 * Six variables
 * (score)
 * (letter#)
 * (letter detect)
 * (list item)
 * (i)
 * (☁ leaderboard)

Encoding
Encoding is the process by which users and their data are formatted into a sequence of numbers in the cloud variable " leaderboard". First, consider the following lists:



Each user's data is linked to his or her score. The scores' numerical placement can be accomplished through scripts outside the encoding and decoding. Using this data linkage, the cloud variable encoder can go one-by-one to add each username and score in number format to " leaderboard".

During the encoding process, each character or number in a list item is represented by a two-digit number. For example, "a" would be the digit "01" and "b" the digit "02". The list "characters" stores as many as 99 characters to encode. If there were 100 or more characters readable by the global high score system, each character would have to be represented by a three-digit value. The order the characters go in this list does not matter; they just have to be consistent throughout the encoding process.

Therefore, each list item (i.e. a user and his or her score) is separated by "00". Without it, the script could not separate the list items properly, and all the characters would be a large jumble.

After the list is filled out with available computer characters, the cloud variable encoding script can be made. It is a long process encoding the data.

define encode set [list item v] to [1] set [☁ leaderboard v] to [] repeat (length of [users v]) set [letter detect v] to [1] set [letter# v] to [1] repeat (length of (item (list item) of [users v])) set [letter detect v] to [1] repeat until <(letter (letter#) of (item (list item) of [users v])) = (item (letter detect) of [letter v])> change [letter detect v] by (1) end if <(letter detect) < [10]> then set [☁ leaderboard v] to (join (☁ leaderboard) (join [0] (letter detect))) else set [☁ leaderboard v] to (join (☁ leaderboard) (letter detect)) end change [letter# v] by (1) end set [☁ leaderboard v] to (join (☁ leaderboard) [00]) set [letter detect v] to [1] set [letter# v] to [1] repeat (length of (item (list item) of [scores v])) set [letter detect v] to [1] repeat until <(letter (letter#) of (item (list item) of [scores v])) = (item (letter detect) of [letter v])> change [letter detect v] by (1) end if <(letter detect) < [10]> then set [☁ leaderboard v] to (join (☁ leaderboard) (join [0] (letter detect))) else set [☁ leaderboard v] to (join (☁ leaderboard) (letter detect)) end change [letter# v] by (1) end set [☁ leaderboard v] to (join (☁ leaderboard) [00]) change [list item v] by (1) end

Decoding
Decoding is the process by which the numerically encoded data is decoded, or taken out of number format and compiled into the two lists again. Decoding checks for the "00" placed between list items during the encoding to determine when to iterate to the next list item. After the process is complete, the lists will be arranged in the manner prior to the encoding.

define decode set [letter# v] to [0] set [letter detect v] to [1] delete (all v) of [users v] delete (all v) of [scores v] add [] to [users v] add [] to [scores v] repeat until <(letter#) > ((length of (☁ leaderboard)) - (1))> repeat until <(join (letter ((letter#) + (1)) of (☁ leaderboard)) (letter ((letter#) + (2)) of (☁ leaderboard))) = [00]> set [letter detect v] to [1] repeat until <(letter detect) = (join (letter ((letter#) + (1)) of (☁ leaderboard)) (letter ((letter#) + (2)) of (☁ leaderboard)))> change [letter detect v] by (1) if <(letter detect) < [10]> then set [letter detect v] to (join [0] (letter detect)) end end if <(letter (1) of (letter detect)) = [0]> then set [letter detect v] to (letter (2) of (letter detect)) end replace item (last v) of [users v] with (join (item (last v) of [users v]) (item (letter detect) of [letter v])) change [letter# v] by (2) end change [letter# v] by (2) repeat until <(join (letter ((letter#) + (1)) of (☁ leaderboard)) (letter ((letter#) + (2)) of (☁ leaderboard))) = [00]> set [letter detect v] to [1] repeat until <(letter detect) = (join (letter ((letter#) + (1)) of (☁ leaderboard)) (letter ((letter#) + (2)) of (☁ leaderboard)))> change [letter detect v] by (1) end replace item (last v) of [scores v] with (join (item (last v) of [scores v]) (item (letter detect) of [letter v])) change [letter# v] by (2) end add [] to [users v]   add [] to [scores v]    change [letter# v] by (2) end delete (last v) of [users v] delete (last v) of [scores v]

Adding and Replacing Scores
When adding and replacing the scores in the lists, a variable is needed to iterate through the list to the correct numerical placement. If the high score leaderboards (consisting of the list "users" and "scores") does not contain the user running the project, his/her username and score will be added to the lists at the beginning of a project. The following script can replicate this situation: when gf clicked decode //category=custom if > //checks if the user is logged in to Scratch add (username) to [users v] add [0] to [scores v] //in which "0" is the initial score encode //category=custom end The example above shows how to add a completely new user to the leaderboards. What if the user is already in the leaderboards but had reached a higher score? To replace a user's current high score with a new one, the current high score must first be deleted. Then, the new high score can be added to the list by iterating down the list with a variable until the score is greater than the one being analyzed. The following script performs this function: decode //category=custom if <[users v] contains (username)> //checks if the user is logged in set [i v] to [1] //begin with the first list item repeat until <(item (i) of [users v]) = (username)> //the deletion process change [i v] by (1) end delete (i) of [users v] delete (i) of [scores v] set [i v] to [1] repeat until <<(i) > (length of [scores v])> or <(score) > (item (i) of [scores v])>> //the variable "score" is the latest score of the user change [i v] by (1) //it will end at the proper list location end insert (username) at (i) of [users v] insert (score) at (i) of [scores v] encode//category=custom

Automatically Resetting Scores
If you want the Global High Score of a game to be reset automatically throughout a period of time so that everyone gets a chance, you can use a script like this one, which resets the score once a day: when gf clicked hide variable [☁ Auto Reset v] forever if > then set [☁ Auto Reset v] to (current [date v]) set [☁ High Score v] to [0]

Avoiding Interferences
When encoding (saving) the data stored in the lists, the cloud variables can take about two seconds to update. If multiple people happen to be encoding at the same time, glitches could occur which cause the data to become broken, changed, or deleted. Also, any disrupted data in the encoder can sometimes cause an infinite loop in the decoder, though scripts can be used to prevent that. These issues are particularly more likely to occur on a very popular project with tons of data to be encoded. To prevent any such encoding interferences from destroying a leaderboard, saving back-ups of the lists often can be very useful.

A script to prevent that can be made but takes time if it is to have a low margin for error. Create a custom block called 'wait and encode'. Also create another cloud variable.

(☁ queue)

define wait and encode repeat until <(☁ queue) = (0)> wait until <(☁ queue) = [0]> wait (pick random (1) to (5)) secs end set [☁ queue v] to (1) encode//category=custom

The higher the second bound of the random wait, the less margin for error there is. What can happen is two computers can still try to encode simultaneously but there is less chance this way.