 Scratch functions have you ever heard of them before you may have seen the my blocks category in scratch 3 But just how do you use it? We'll stick around because in this scratch my blocks tutorial will make a custom block or 2 to fade in and fade out a sprite And we'll see what it means to run without screen refresh coming up Hello world surfing scratchy here teacher server programmer bringing you the goodness of learning to carry through video tutorials If it sounds like something that you're into then consider hitting that subscribe button Hit the show more button below to check out the show notes for links that relate to this video I've also got a link to my funky tea here if you go suss that out and purchase it You'll be supporting this channel big ups from me to you, but hey, let's go make some custom blocks I'm in the scratch blocks explain project You won't need it for this tutorial But I'll send you a card up in the top corner now to go check it out if you want to go across the categories And click the pink my blocks my blocks in scratch are like functions in other programming languages And functions have a couple of main purposes first is to group a set of instructions and Another is to create a set of instructions that are Reusable that you want to reuse throughout your project. We'll explore those types in action a little later on But first let's go ahead and create our first block hit this make a block button here You can give your block a name. It's good to start a block name with a verb So like a doing action something like do Something hit okay, you'll see here that we've got this hat block here in our code editor and over in our blocks We've also got this stack block. It is the name of our function do something Let's go ahead and get our cursor block to do something to jump over to the looks and we are going to show So what this will do is when we press do something we will show our road Now it's not very useful at the moment because there's already a block here to show the road So just creating a custom block to do something that already exists doesn't really make a whole lot of sense So I think what we should do is let's create an animation to fade in at the road and fade out the road Head on over to your control blocks track out a repeat ten times block You can get rid of that show block now head on over to your looks and now we're going to change The ghosting effect and we're going to change the ghosting effect by 10 and actually this one is going to be negative 10 Okay, so this block now fades in but it still says do something So how about we right-click it and we edit it and now we just Rename it to fade in now if I drag out this block and I click it it fades in but you can't actually see it So what we need to do now is create a fade out block. Let's make a block let's call it fade out and Basically want to do the exact same thing But instead of negative 10 we want to go up by 10 and now I can fade out So this will fade out the road and now we can fade in the road So that's a nice little animation that we've created and what the custom block has done is basically create a shortcut to these sets of Instructions I'm looking at this and we've got some duplication happening and where we have duplication That's a great indicator for us to use a custom block. So let's go ahead and create a new block We're just going to call it fade and press okay So we've got a new block definition right there and what we're going to do is We're going to duplicate this code and we're going to look at what's called Parameters in other languages what that means is we're going to pass some values to our fade block We're going to pass the number of repeats and the amount that we change the effect by so let's press Edit and let's go ahead and add some of these inputs So we're going to add it to inputs the first input is going to be the repeat times and the second input that we're going to have is the Change amount go ahead and click okay So you'll see here that it's created two reporter blocks and we can drag those out and put them as values in the Spaces provided so repeat the number of times that it gets passed in and let's now change the change amount So what we can do is get this fade stack block that we have now and where we see the number ten for fade in I will write ten where we see negative ten for the change amount We can go ahead and actually just do that so our fade in calls the fade block We can duplicate this and when we see ten is the same But we're no longer negative ten We can actually just make that ten and we can get rid of that code So now we just have the repeat C block with the repeat times parameter And we have the change stack block with the change amount value there I'll go ahead and press fade in and you'll see our road reappear and I'll press fade out once again Now we can tweak these values so we can make the animation faster or slower So let's repeat it 100 times and instead of changing by ten. Let's change it by one So if I press fade out, let's fade in first time So if I press fade out, you'll see the road gradually disappear So I'm going to fade that back in and then we'll fade out Now I'm looking at these values and if I would look at this for the first time This would be a little bit confusing to me So what we'd want to go ahead and do is in our fade definition is to create some labels here So we can tell anyone else who looks at our code what these actually are so let's edit it And what we can do is we're going to know that we have these repeat times and change amounts so fade number of times and Then we also want to add the label change amount Change amount. We also want to add the value change amount press ok and Look what has happened here. So in our to fade in and fade out blocks You'll see fade number of times is ten and the change amount which Here was negative ten Fade number of times 100 and changing the amount by one so we've been good And we've been passing in numbers here, but you can also pass in some text and if you were to pass into some text here We're not currently handling for that situation So that's just something to consider if we right-click and go into edit You'll see that an input field can contain either a number or some text You can also add in an input boolean. So in our example, you could just pass in a boolean to say Hey, are we fading in you can go like fade in and if that was equal to true Then we would have a fade in animation and if it was equal to false You could just say fade out so you could handle all that logic in the fade Let's go and have a look at what this run without screen refresh does to do that I'm going to jump into another project Here we are in this project and what this project does is it just creates this nice checkered grid here You'll see that I've used some custom blocks. I've used one custom block to draw a pixel I've got one single sprite here. That is a square and I've called this a pixel So what this does is it draws pixels in rows across the screen and it has a starting position Starting X position and a starting Y position. I've also defined the pixel size, but we're here to talk about screen refresh So if I right-click this and I go edit we're talking about this little label here So what does this actually mean? There's a very tiny amount of delay that happens between two blocks executing scratch Uses this to update the screen So what you can do is if you tick this it will no longer update the screen So just to give you an idea of what this actually does is I'm just going to press the stop sign here And watch what happens when I press the green flag. We've got our grid building in here And it takes a little bit of time. All right So I'm just going to press the stop sign if I go back in and I right click edit and I run this without a screen refresh Which what happens now? I press the green flag and our grid just appeared So what it did is it removed all the delays and it just went boom here is the grid so far We've looked at my blocks as using repeatable code and passing in inputs as parameters There is another way that you can use my locks going to switch back into the other project here We are back in scratch blocks explain. I'm just going to jump into this car sprite Now another way that we can use custom blocks is just to group and name a set of instructions So you'll see here that we've got this long list of instructions and you can go through and figure out what it does You probably just press it But it's really nice and useful to just be able to read something and just take it sort of for granted What it should do so I know these sets of instructions actually tell these cars to go around the track So I'm going to create a block and I will say go around track Notice how I'm still using a verb there go around the track click okay And what we can do is grab all of those instructions and put that right under that Definition and now we can repeat going around the track three times So now if somebody looked at my project and they went through my code They could just look at that and go repeat three times going around the track I strongly encourage you to use my blocks as often as you can because it really helps make your code look really clearer And it'll help you in the future if you're looking at an old project and trying to remember what it does to help you Remember what you did start for scratchy question And I want to know have you got any experience using the my box category tell me about it in the comments section below Hey, thanks for checking out this scratch my blocks tutorial like subscribe ring that bell if you're around here And have a scout of some of the other content on your screen right now You can support surfing scratch it by checking out my patreon page and sussing out some of these funky teas in the description below But until then I'm off to go find a wave. I'll catch in the next one