 Have you hit a wall working with Scratch Clones? Maybe you've been trying out local variables, but hey, your project's a little bit buggy and you're just not sure where to go. Well, check out this tutorial for advanced Scratch Clones that uses local variables. We'll use a basic gun thread example to help build our understanding. Stick around till the end too, and I'll share with you a basic game that you can make that'll help take your clones to the next level. 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 just like you. If you dig in this content, be consider hitting that subscribe button to stay in the loop. And hey, if you're an educator and you want to join the mailing list, that's where I'll send news and resources straight to your inbox. Link below in the description. All right, let's make some clones. All right, let's make some clones. All right, let's make some clones. I have to do that. All right, for real, let's do it. All right, I'm just in a new Scratch project here, and I've just got three sprites to ret, to ret gun, and a bullet. And the only code that I've got here is a forever loop. And forever, we're pointing the Tourette gun in the direction of the mouse pointer. We're going to get some bullets firing from this Tourette gun. We're going to use these bullets to demonstrate some things that we can do with clones. I'll have a link to this startup project down in the description if you want to start with it. All right, let's start making some clones. What we're going to do is we're going to grab a, when the space key is pressed, when the space key is pressed, go down to the control and we'll grab out a creator clone. Now if I press the space key, you can see it executes, but nothing happens. The reason for that is I've currently got it hidden. So we need to grab the block when we start as a clone. We want to show that clone, and let's test that out. You can see the clones are forming down here at the bottom of the Tourette. That's not exactly what we want. We want to send the clone to the position of the Tourette gun, and then we want to point it in the direction of the mouse. So let's go do that. In the motion category, grab out the go to random position block and change random position to Tourette gun. Then we want to point in the direction of the mouse pointer. If we head over to the stage and we press the space bar, and you can see it's kind of printing some clones here, which is kind of neat. I've created a sun effect here. I just had a little bit of fun and trying to make a sun. Okay, this is no good to just have static balls here. We want them to move across the stage. So to do that, let's head over and grab a repeat until block. We're going to repeat until we're touching not the mouse pointer, but the edge of the stage. And we're going to repeat moving, let's say, five steps. Let's test that out. Now it won't work at the moment because currently these clones here are touching the stage. So if we stop the project and restart it, and we hit some bullets, there we go. And bullets stop. What we can do is delete the clone once it hits there. So if we head back over to the control category, scroll down and click delete this clone. Fire it, and there it goes. So this is a basic implementation of cloning. There's some more advanced stuff that we can do. And that includes sensing. What do I mean by that? That means sensing or looking at all the attributes of the individual clones. To do that, we need to use a combination of global variables and local variables. To help us understand these more visually, let's jump over to the variables category. We're going to click make a variable. You'll notice that we've got the options for all sprites and for this sprite only. We want to link variables specifically to clones and have clones have their own identical attributes. We want to go for this sprite only. This creates what's called a local variable, local to the sprite. I'm going to call this bullet ID and click OK. You'll see here on the stage, we've got a bullet and a colon sign and then bullet ID. You'll see over here in the left code block pane that we've got the bullet ID variable. If I click to write gun, that bullet variable goes. So that just shows you that it belongs specifically to the bullet. You can see here that the value of bullet ID is set to zero. What we're going to do is when we start as a clone, we're going to set the bullet ID to a random number between let's say 10 and 20. Usually you just probably change the ID by one, but to demonstrate this, we're just going to pick a random number between 10 and 20. Okay, then to see this in action, I've just got to say block from the looks category. I've got a join block that's going to join the bullet ID and the value of bullet ID. And I'm just going to grab that block and going to squish it between the show and the set block. Now, if I fire off a bullet, you'll see that the ID is 14. If I fire off another bullet, okay, 14 again. And then there's 13. But if you look up here in the top corner, the bullet ID is set to zero. So what's going on there? Would you expect this to change? Well, it doesn't because this bullet ID refers to this single sprite here. And all the IDs that you're seeing belong specifically to that clone. Here's our mum or dad, and all of our bullets are like the children. Just to really underscore this, I'm going to make a new variable. I'm going to call it a global ID. And this time it's going to be for all sprites. I'm going to click okay. Now, when we start as a clone, we're going to set the global ID to that random number. And then we're going to set the bullet ID to that same random number. Now, watch what happens as we shoot some bullets. I'll aim up here at the global ID. I'm going to press the space bar, and you see 19 and 19. So the ID of the clone was 19, and the ID of global ID is 19. So because it's a global variable, there's only one instance of it. And it is the same instance regardless of which clone we are operating in. Notice that the bullet ID of the sort of mum or dad sprite is still remaining at zero. So what I've gone ahead and done now is drag out a when the green flag is clicked block, and we're going to set the global ID to zero. And rather than setting the global ID to a random number, we're going to change the global ID by one. This is going to help us keep a record of all the clone numbers and their IDs. The reason I'm doing this is to demonstrate how we can access sprites when we click them. So I'm going to disconnect this, delete this clone block, and I'm going to drag out a when the sprite is clicked. And instead of saying the bullet ID when we start as a clone, we're going to say the bullet ID when this sprite is clicked. I'll just scroll across so you can see that. Okay, let's just demo this now. So I'm going to press the green flag. We set global ID to zero, and we will fire a bullet. Bullet hits the stage, we click it, and bullet ID is set to one. Going to fire it again. You'd expect this to be two, and that is cool. If we look up here, global ID is set to three, which is what happens when I fire another bullet. Fire a bullet, and global ID is now seven. So really, this sprite here should be seven. But it's four? Huh, what's going on here? One, two, four. What happens to this sprite? Eight. It looks like it's doubling each time. So what's going on here? Drop a comment down below if you already know what's going on. So the global ID tells us the number of clones that we've made, but we can only see four here. Yeah, we can only see four, but this number here tells us how many are stacked there, so there's actually eight here. There's four there, there's two there, and there's one single clone here. To prove that to you, I'm just going to click these clones here, and you can see I'm dragging them off, and it's getting a little bit unwieldy. The reason for that is every time we hit the space key, we create clones of clones. So there's an issue. We're going to jump over to our variables category. We're going to create a new variable, and we're going to call it isClone with a question mark, and we're going to create it for this sprite only. When the green flag is clicked, we want to set isClone to no. Then when the space key is pressed, we only want to create a clone of ourselves if isClone is set to no. So in our control blocks, if isClone is equal to no. The last thing that we need to do is when we start as a clone, we need to set isClone to anything but no. So we'll just say yes for consistency here. Why does this work? Well, when we start as a clone, the value of isClone for all the clones will be set to yes, which means when we press the space bar, it will not execute the clone code. Let's go get our full bullets back on the stage. And we'll go through and click them now. So there is bullet one, there is bullet two, there is bullet three, and there is bullet four. You see our global ID is set to four as well. So all is well in clone land. So that's great. But you still may have some skepticism. Why go to all this pain in the first place? Well, the reason is that you may want to do something specific on each individual clone. And now that we have an ID, we can refer to it. So let's do something kind of novel. Let's just go into our control category here. We'll go if the bullet ID is equal one. So that's going to be this bullet over here. I'm going to go over to the looks category. And I'm just going to get it changed the color effect by 25. So now when I click this bullet here, it'll change its color. But if I click the other bullets, their colors won't change. So we can specifically refer to a clone. And that's why this is so handy. Using IDs in this way can be particularly useful when we use lists to store the values. So we can store lists for all the expositions. And we can extract an exposition based on the ID of the clone. But I'm going to save that for another video, which you can see the card up in the top corner for right now. All right, it's time for a scratchy question. And I want to check your understanding this week. Share in the comment section down below one situation where you might want to use local variables with clones. Thanks for checking out this tutorial on scratch clones and local variables. If you found it useful, hit that like button. And if you're really digging this content, consider joining the surfing scratcher mailing list. Link below in the description. Down there, you'll also find a link to this funky tea on Redbubble. But hey, until then, I'm off to go find a wave. I'll catch you in the next one.