Global High Scores
Шаблон:Cloud Data Шаблон:About 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
Simple high score
For a simple one that shows the highest score, two variables can be used:
The following script can then be created:
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:
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
- Nine variables
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 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.
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.
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:
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:
Then, use this:
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.
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.