 Have you been dreaming Scratch Cloud variant? Probably not. But hey, you might want to use them in your next Scratch project. I'm going to go through what they are, a few gotchas, and how to store text in the cloud. Hint, we'll encode and decode the music numbers. Promise, it's not as hard as it sounds. All this coming your way. Hello world, Surfing Scratcher here, teacher-surfer programmer, and on this channel I make how-to coding videos for curious learners and educators. If you dig in this content, then consider hitting that subscribe button to stay in the loop. And hey, if you're an educator and you want user resources sent straight to your inbox, consider joining the mailing list linked below in the description. But hey, let's get stuck in the cloud variables. Alright, I'm just on the Scratch info page, and the frequently asked questions about cloud data just to go over a couple of key things. What are they? Cloud variables can be saved and shared with other people in the Scratch community. The data associated with your interactions can be stored along with your username and others can view it. I've just got an example of some cloud variables that I've got in a project of mine. You can see here where I've been setting the variables and where I've been renaming it or creating it. You can access that data page that you just saw on the project page where you see cloud variables and hitting see data. Okay, let's jump back over to the facts. This is a crucial sentence. Only numbers can be stored in cloud variables. Yep, that means no text. Only numbers. And the last point I want to mention here is if you're a new Scratcher, then you won't be able to access cloud data. It's only available to accounts that have the status of Scratcher. Where can you find that out? Head over to your profile page, and in the top left corner, you'll see your status. Here, I've got a teacher account, which makes me a Scratcher. I'll leave a link in the description where you can suss out these facts and also the Scratch Wiki on cloud data. Very useful resource. Okay, let's start making some cloud variables. I'm here in the Scratch project editor. If you're wondering about this project, I've got a card in the top corner that will show you the video that made it. Otherwise, you can grab the starter project in the description. You won't need it, but hey, it might be more useful if you go and get it. In this game, we press the green flag, and we've got a race against the clock to complete the end of this sentence. We've put some errors that we can log in there as well. And once we finish our time, we'll stop. Now, what we'd like to do with our cloud variables is store this time as well as the username in the cloud so that we can keep score and so multiple people complete this game and maybe even build a leaderboard in the future. First thing we'll do is create this cloud variable to store the time. So we're going to head over to make a variable. We're going to check the cloud variable box. And the name that we'll call this is rank one time. Okay, it gives us a little reminder that cloud variables only support numbers. Okay, so I've created the cloud variable rank one time. I'm just set it to a default value of 100 using the slider. Now, I'm the owner of this project, and I'm the only one who can change this. If you remix this project, you'll create a copy of this variable and you will not be able to manipulate it directly. Okay, so I've just got some logic here in a new statement. If the current time plus the errors, we're just going to add the errors onto the time is less than the current rank one time. Well, then we want to set the rank one time to what we have here. So to set our cloud variable, it's just as simple as grabbing a set block and setting it to what we want it to be. If I click this, we'll set rank one time to 11.423 seconds plus two, which is 13.423 seconds. Then if I go to the project page of this project and we click down here, see data, the cloud variables, you'll see that we've created that variable eight minutes ago and then we just set it. Let's head back into the project. Okay, just for those of you who are following through this project, I'm just going to grab that if and else block now. I've just created an if and else block and I'm just going to connect it to the end of the start game function. Story number, piece of cake, right? What about storing your username? That's a little bit trickier because your username is made up of characters and we can't store characters in the cloud. What do we do? We need a way to encode our username to turn it into numbers so then we can store it in the cloud and then when we boot up the project, we need to decode it so then we can print the text values again. I suggest we create a couple of blocks. Let's create our first one. We're going to call this one encode username and we're going to have an input there of username. The reason I'm using an input here is so I can test it so I can bring this out and put in anything that I want just to make sure it's working. But for now, I'll put my username in there. Okay, the next thing that we're going to need to do is to create a list. I'm going to make this a constant list. I'm just going to underscore that by writing it in capital which means I'm not going to change it. I'm going to make it for all sprites and click okay. The next thing I want to do is right click and import and I've set up a text file here with all the characters that I'll need. Now in the spaces from one to nine, I've put in characters that aren't eligible for usernames because we don't actually need them. So we'll use the digits from 10 onwards and I'll explain the reason for that in a little bit. So we're going to use this list to encode the letters of our username here. So if I click my username, we'll look for the S and we'll scroll down and the letter S will be given the number 28. And we'll do that for every single character and we will create a long number as a result. We're going to create a variable and I'm going to call it 10th username. We're going to use this temporary username to both encode and decode. It'll be for all sprites. I'm going to set 10th username to an empty field. This will default to zero or one probably so make sure it's empty. Next, we want to grab this I variable. If you don't have one, just make a new variable and call it I. This is our index variable. I'm going to change that here and we want to set index to one. Now we want to repeat the length of the username. Now we want to find the item number of the characters in our username. So we need to go into the variables box. If I change our input here to s and I click this, you'll see that s is equal to 28 which aligns with what we're going for. So we can iterate over each character and we will use this I variable to do that. So we'll step up, we'll change I by one each time and we will extract this item number of the character. But instead of s, we actually want username but we don't want the whole username. We only want a letter of the username. So we want a letter of the username and what letter do we want? We want a letter that I is currently at. So we'll insert that into our block and this is what we're looking for. Next we need to find a join block and we're going to join the current value of temp username to the letter that we extract. So the current value of temp username is nothing. When we extract the first letter, we'll join the s to nothing and we'll always join the next letter to the end of this string. To round off this block, we want to set temp username to that block that we just created to change I by one. Okay, if I click this now, if we encode the username, you'll see here temp username on the screen turns into a really long number. I don't know if this has actually worked. To figure this out, we need to decode this number now. The last thing we want to do here is to create another cloud variable and we'll call this one rank one username. We want to set rank one username to the temp username. Then what I've gone ahead and done is just create another custom block, decode number to username and what I'm passing in is that rank one username as a parameter. I've just gone ahead and duplicated the code that was in the encode but we're going to tweak things slightly. So first up, we're going to be using the temp username and the I variable again. We're going to also repeat but we're not going to repeat for the length of username. We're going to be repeating for the length of the number. Now, we don't want every single number. We want to repeat for half of the total numbers here because we're actually going to be counting these numbers in twos. Remember before how I said I'd explain the importance of not using the first nine digits. Well, if we did, we wouldn't be quite sure if we need to count in twos or ones. So if we just use two digit numbers then we know we can just count in twos each time. Okay, so we're just going to get the length of number and we'll divide that number by two because that's how many times we want to repeat this. Don't need that anymore. And we're going to be changing this up slightly. Okay, this is going to seem pretty complicated but we'll just break it down step by step. So the first thing that we want to do is extract the first two digits of the temp username. To do that, we can take whatever i is equal to and then i plus one. And that will give us the first two digits and we can join those two together. So we'll get the letter of i number. So this will give us the first digit of the number. Then we can just duplicate that but instead of getting only i we need i plus one. Okay, if I click this, it won't work because nothing because number is undefined. But if we change i by two then we'll get every second digit and we're always using the current value of i as our reference point. So since this block of code is going to return the item number from our charlist, we need to go get what the value is. So this item of charlist is exactly what we'll need to return a letter value for our username. Now that we have that letter, we need to join that letter to the current value of temp username. We're going to put this inside temp username. And now with any luck, when I press this, our temp username should turn into surfing scratcher. Boom, and it does. We've just encoded and decoded our username. The last thing I've done is just add a new variable called scratcher and we're just going to set scratcher to the value of temp username after decoding the username. Then when the green flag is clicked, we can just decode the username that we have in storage in the cloud variable. And when we start the game, if we're successful, we can just encode the username if we beat the current rank one time. We'll also want to decode that number to the value of the new scratcher who won. That's how you encode and decode text to store in the cloud on scratch. Now I've just set the values for one username. I'd be interested to see you guys out there remixing this project and see if you can create a leaderboard of say the top 10 positions of people in this game. You could use 10 different cloud variables to do that. We'll see how you go storing all the usernames in one single cloud variable and you could separate that using a comma. If you manage to do that, drop your solution in the comment section below. I'll port it across and I'll post your name in the credit section of the scratch project. Alright, time for a scratchy question and I want to check your understanding. Based on this video, why couldn't you use the first nine slots in the charlist to encode and decode the text variable? Comment with your answer down below. Alright, you made it. Thanks for checking out this tutorial on scratch cloud variables. We found it useful. Hit that like button and if you want news and resources sent straight to your inbox, consider joining the surfing scratcher method link below in the description, I'll put a link to my funky red bubble tea. But until then, I'm off to go get a wave. Catch you in the next one.