(Redirected from World Record Setter)

(☁ 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.
This article is about global high scores. For strictly encoding and decoding, see Encoding and Decoding Cloud Data.

This tutorial shows how to record global high scores with Cloud Variables.

Simple high score

For a simple one that shows the highest score, two variables can be used:

  • (score)
  • (☁ high score)

The following script can then be created:

when gf clicked
forever
if <(score) > (☁ high score)> then
set [☁ high score v] to (score)


Note Note: This script shows only the highest score, not the username of the person who set it.

Simple world record

For a simple one that records the lowest completion time, two variables can be used:

  • (time)
  • (☁ record time)

The following script can then be created:

when I receive [win v]
if <(time) < (☁ record time)> then
set [☁ record time v] to (time)


The "record time" cloud variable must be set to a number higher than the expected time before the project is published. Alternatively, it can be set to a time the project maker had if competition is encouraged from the beginning.

Note Note: This script shows only the lowest completion time, not the username of the person who set it.

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:

when gf clicked
hide variable [☁ Auto Reset v]
forever
if <not <(☁ Auto Reset) = (([floor v] of (days since 2000)::operators)*(number of wanted time in a day::grey))>> then
set [☁ Auto Reset v] to (([floor v] of (days since 2000)::operators)*(number of wanted time in a day::grey))
set [☁ High Score v] to [0]

Ranked Records

Since cloud lists are rejected, programming a leaderboard with multiple positions 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.

Requirements

This tutorial requires the use of:

  • Three lists
    • letter::list reporter
    • users::list reporter
    • scores::list reporter
  • Nine variables
    • (score)
    • (letter#)
    • (letter detect)
    • (list item)
    • (i)
    • (☁ leaderboard)
    • (v1)
    • item::variables reporter
    • chars::variables reporter

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:

Data Compare.png

Each user's data is linked to their 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 their 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 [v1 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 [v1 v] to (join (v1) (join [0] (letter detect)))
        else
            set [v1 v] to (join (v1) (letter detect))
        end
        change [letter# v] by (1)
    end
    set [v1 v] to (join (v1) [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 [v1 v] to (join (v1) (join [0] (letter detect)))
        else
            set [v1 v] to (join (v1) (letter detect))
        end
        change [letter# v] by (1)
    end
    set [v1 v] to (join (v1) [00])
    change [list item v] by (1)
end
set [☁ leaderboard v] to (v1)

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.

Note: Scratch 3.0 introduced a 1/10 of a second cloud update

define decode
set [letter# v] to [0]
set [letter detect v] to [1]
delete all of [users v]
delete all 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 (length of [users v]) of [users v]
delete (length of [users 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, their 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
delete all of [letter v]
set [chars v] to [� !"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~0123456789
set [item v] to (1)
repeat (length of (chars::variables))
add (letter (item) of (chars::variables)) to [letter v]
change [item v] by [1]
end
decode::custom
if <<not <(username) = []>>and<not<[users v] contains (username)?>>> then //checks if the user is logged in to Scratch and not in the leaderboard
add (username) to [users v]
add [0] to [scores v] //in which "0" is the initial score
encode::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:

define change leaderboard
decode::custom
if <[users v] contains (username)> then //checks if the user has been added to the leaderboard already
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::custom

Then, use this:

when gf clicked
delete all of [letter v]
set [chars v] to [� !"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~0123456789
set [item v] to (1)
repeat (length of(chars::variables))
add (letter (item) of (chars::variables)) to [letter v]
change [item v] by [1]
end
decode::custom
if <<not <(username) = []>>and<not<[users v] contains (username)?>>> then //checks if the user is logged in to Scratch and not in the leaderboard
add (username) to [users v]
add [0] to [scores v] //in which "0" is the initial score
encode::custom
end
forever
change leaderboard::custom
end

Avoiding Interference

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 interference 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::custom
set [☁ queue v] to (0)

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.

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