m (minor change.)
m (Encoding: you're welcome)
Line 16: Line 16:
  
 
==Encoding==
 
==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:
+
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:<br />
 
+
[[File:Data Compare.png]]<br />
[[File:Data Compare.png]]
 
 
Each user's data is linked to his or her score. The scores are already placed in order.
 
Each user's data is linked to his or her score. The scores are already placed in order.
  

Revision as of 16:33, 1 December 2013

Warning.png This article or section is currently undergoing major changes by Turkey3 (talk | contribs). Please avoid largely modifying this page's contents until this template has been removed. (Date?)

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.

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
    • "characters"
    • "users"
    • "scores"
  • Four Variables
    • letter#
    • character
    • list item
    • ☁ 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:
Data Compare.png
Each user's data is linked to his or her score. The scores are already placed in order.


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 [prestiges v]))
        set [letter detect v] to [1]
        repeat until <(letter (letter#) of (item (list item) of [prestiges 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
define decode
set [letter# v] to [0]
set [letter detect v] to [1]
delete (all v) of [users v]
delete (all v) of [prestiges v]
add [] to [users v]
add [] to [prestiges 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 [prestiges v] with (join (item (last v) of [prestiges v]) (item (letter detect) of [letter v]))
        change [letter# v] by (2)
    end
    add [] to [users v]
    add [] to [prestiges v]
    change [letter# v] by (2)
end
delete (last v) of [users v]
delete (last v) of [prestiges v]