(Moved section and changed color)
 
(25 intermediate revisions by 12 users not shown)
Line 1: Line 1:
:''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 [[#Encoding|this]] section.''
+
{{Cloud Data}}
 +
{{about|global high scores, although it focuses on encoding and decoding|strictly encoding and decoding|Encoding and Decoding Cloud Data}}
 +
With the new [[Cloud Variables|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.
 +
{{note|Though this focuses on high scores, this information can also be relevant to any user-saved data.}}
 +
 
 +
 
 +
==Simple high score==
 +
For a simple one that shows the highest score, two variables can be used:
 +
* <sb>(score)</sb>
 +
* <sb>(☁ high score)</sb>
 +
 
 +
The following script can then be created:
 +
 
 +
<scratchblocks>
 +
when gf clicked
 +
forever
 +
if <(score) > (☁ high score)> then
 +
set [☁ high score v] to (score)
 +
</scratchblocks>
  
With the new [[Cloud Variables|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.
 
{{Note|Though this focuses on high scores, this information can also be relevant to any user-saved data.}}
 
  
 +
{{note|This script shows only the highest score, 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:
 +
<scratchblocks>
 +
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]
 +
</scratchblocks>
 
==Requirements==
 
==Requirements==
Since cloud [[list]]s 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:
+
Since cloud [[list]]s are rejected, 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
+
* Three lists
**letter
+
** <sb>letter::list reporter</sb>
**users
+
** <sb>users::list reporter</sb>
**scores
+
** <sb>scores::list reporter</sb>
<!-- wiki doesn't have  :: list yet -->
+
* Nine variables
*Six variables
+
** <sb>(score)</sb>
**<sb>(score)</sb>
+
** <sb>(letter#)</sb>
**<sb>(letter#)</sb>
+
** <sb>(letter detect)</sb>
**<sb>(letter detect)</sb>
+
** <sb>(list item)</sb>
**<sb>(list item)</sb>
+
** <sb>(i)</sb>
**<sb>(i)</sb>
+
** <sb>(☁ leaderboard)</sb>
**<sb>(☁ leaderboard)</sb>
+
** <sb>(v1)</sb>
 +
** <sb>item::variables reporter</sb>
 +
** <sb>chars::variables reporter</sb>
  
 
==Encoding==
 
==Encoding==
Line 36: Line 65:
 
define encode
 
define encode
 
set [list item v] to [1]
 
set [list item v] to [1]
set [☁ leaderboard v] to []
+
set [v1 v] to []
 
repeat (length of [users v])
 
repeat (length of [users v])
 
     set [letter detect v] to [1]
 
     set [letter detect v] to [1]
Line 46: Line 75:
 
         end
 
         end
 
         if <(letter detect) < [10]> then
 
         if <(letter detect) < [10]> then
             set [☁ leaderboard v] to (join (☁ leaderboard) (join [0] (letter detect)))
+
             set [v1 v] to (join (v1) (join [0] (letter detect)))
 
         else
 
         else
             set [☁ leaderboard v] to (join (☁ leaderboard) (letter detect))
+
             set [v1 v] to (join (v1) (letter detect))
 
         end
 
         end
 
         change [letter# v] by (1)
 
         change [letter# v] by (1)
 
     end
 
     end
     set [☁ leaderboard v] to (join (☁ leaderboard) [00])
+
     set [v1 v] to (join (v1) [00])
 
     set [letter detect v] to [1]
 
     set [letter detect v] to [1]
 
     set [letter# v] to [1]
 
     set [letter# v] to [1]
Line 61: Line 90:
 
         end
 
         end
 
         if <(letter detect) < [10]> then
 
         if <(letter detect) < [10]> then
             set [☁ leaderboard v] to (join (☁ leaderboard) (join [0] (letter detect)))
+
             set [v1 v] to (join (v1) (join [0] (letter detect)))
 
         else
 
         else
             set [☁ leaderboard v] to (join (☁ leaderboard) (letter detect))
+
             set [v1 v] to (join (v1) (letter detect))
 
         end
 
         end
 
         change [letter# v] by (1)
 
         change [letter# v] by (1)
 
     end
 
     end
     set [☁ leaderboard v] to (join (☁ leaderboard) [00])
+
     set [v1 v] to (join (v1) [00])
 
     change [list item v] by (1)
 
     change [list item v] by (1)
 
end
 
end
 +
set [☁ leaderboard v] to (v1)
 
</scratchblocks>
 
</scratchblocks>
  
Line 79: Line 109:
 
set [letter# v] to [0]
 
set [letter# v] to [0]
 
set [letter detect v] to [1]
 
set [letter detect v] to [1]
delete (all v) of [users v]
+
delete all of [users v]
delete (all v) of [scores v]
+
delete all of [scores v]
 
add [] to [users v]
 
add [] to [users v]
 
add [] to [scores v]
 
add [] to [scores v]
Line 111: Line 141:
 
     change [letter# v] by (2)
 
     change [letter# v] by (2)
 
end
 
end
delete (last v) of [users v]
+
delete (length of [users v]) of [users v]
delete (last v) of [scores v]
+
delete (length of [users v]) of [scores v]
 
</scratchblocks>
 
</scratchblocks>
  
 
==Adding and Replacing Scores==
 
==Adding and Replacing Scores==
When adding and replacing the scores in the lists, a variable is needed to iterate through the  
+
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:
 
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:
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
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
 
decode::custom
if <not <(username) = []>> then //checks if the user is logged in to Scratch
+
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 (username) to [users v]
 
add [0] to [scores v] //in which "0" is the initial score
 
add [0] to [scores v] //in which "0" is the initial score
Line 129: Line 166:
 
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:
 
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:
 
<scratchblocks>
 
<scratchblocks>
 +
define change leaderboard
 
decode::custom
 
decode::custom
if <[users v] contains (username)> then //checks if the user is logged in
+
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
 
set [i v] to [1] //begin with the first list item
 
repeat until <(item (i) of [users v]) = (username)> //the deletion process
 
repeat until <(item (i) of [users v]) = (username)> //the deletion process
Line 145: Line 183:
 
encode::custom
 
encode::custom
 
</scratchblocks>
 
</scratchblocks>
 
+
Then, use this:
==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:
 
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
hide variable [☁ Auto Reset v]
+
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
 
forever
if <not <(☁ Auto Reset) = (current [date v])>> then
+
change leaderboard::custom
set [☁ Auto Reset v] to (current [date v])
+
end
set [☁ High Score v] to [0]
 
 
</scratchblocks>
 
</scratchblocks>
 +
==Avoiding Interference==
 +
When encoding (saving) the data stored in the lists, the cloud [[variable]]s 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 [[wikipedia:Infinite loop|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.
  
{{note|You can modify the "date" option to the desired time frame in which you want the scores to be reset. For example, if you want the scores to be reset every "hour" change the option to "hour" }}
+
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.
 
 
==Avoiding Interferences==
 
When encoding (saving) the data stored in the lists, the cloud [[variable]]s 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 [[wikipedia:Infinite loop|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.
 
  
 
<sb>(☁ queue)</sb>
 
<sb>(☁ queue)</sb>
Line 178: Line 222:
  
 
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.
 
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.
 +
 
[[Category:Scripting Tutorials]]
 
[[Category:Scripting Tutorials]]
 +
 +
[[fr:Scratch Wiki Accueil/tutos/013 ajouter highscore]]

Latest revision as of 13:34, 2 August 2021

(☁ var) This article or section is about or uses Cloud Data. Users with the New Scratcher status or ones who are using the Offline Editor cannot make projects using Cloud Data or use it in other users' projects. New Scratchers need to have Scratcher status. Offline editor users need to use the online editor.


This article is about global high scores, although it focuses on encoding and decoding. For strictly encoding and decoding, see Encoding and Decoding Cloud Data.

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.

Note Note: Though this focuses on high scores, this information can also be relevant to any user-saved data.


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.

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]

Requirements

Since cloud lists are rejected, 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::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 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 [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.

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, 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
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.