Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to use the custom blocks? When do we need to use it and why?
- Srijani18
-
Scratcher
55 posts
How to use the custom blocks? When do we need to use it and why?
Hello!
I've been trying to use the custom blocks for a long time but I'm not able to.
Please help with this.
Thanks
Srijani18
I've been trying to use the custom blocks for a long time but I'm not able to.
Please help with this.
Thanks
Srijani18
- WiktorMe
-
Scratcher
9 posts
How to use the custom blocks? When do we need to use it and why?
A custom block can be used to organise your scripts or to use a script many times in your project. To use it, click “Create a Block” in the “My Blocks” section. Then, you can name your block. Then, click OK. Once it has been created, a “Define” block will appear. Under that block, place what you want the block to do. When you use the custom block, the code under the Define block will run.
- deck26
-
Scratcher
1000+ posts
How to use the custom blocks? When do we need to use it and why?
There are a couple of advantages. Custom blocks can be set to run very fast by clicking the ‘no screen refresh’ option. Just don't include any real or implied wait blocks (eg glide implies a timed operation) or the block will lag badly with that option. So redrawing several things on screen with the pen can be done almost instantly, for example.
Second advantage is being able to pass parameters. Say you had one bit of your project that needed to work out the largest value from a, b or c and another the lrgest from d, e and f. A custom block with 3 parameters can store the largest of those values to a ‘return’ variable. To do that with a broadcast would require setting the three variables the receiver refers to to the values.
Also, clones will run broadcasts so if you want a cloned sprite to run a block of code just for that object you can run a custom block and only that object will run the code. So if you have 10 enemy clones and you want them to do something when they touch the player a custom block can be run when a clone detects the touch.
Second advantage is being able to pass parameters. Say you had one bit of your project that needed to work out the largest value from a, b or c and another the lrgest from d, e and f. A custom block with 3 parameters can store the largest of those values to a ‘return’ variable. To do that with a broadcast would require setting the three variables the receiver refers to to the values.
Also, clones will run broadcasts so if you want a cloned sprite to run a block of code just for that object you can run a custom block and only that object will run the code. So if you have 10 enemy clones and you want them to do something when they touch the player a custom block can be run when a clone detects the touch.
- conor67
-
Scratcher
30 posts
How to use the custom blocks? When do we need to use it and why?
Custom Blocks is a very good way to shorten down lots of blocks, For example
"Text (string) at speed of (string)
but can be used to organise your scripts or to use a script many times in your project. To use it, click “Create a Block” in the “My Blocks” section. Then, you can name your block. Then, click OK. Once it has been created, a “Define” block will appear. Under that block, place what you want the block to do. When you use the custom block, the code under the Define block will run.
Also, clones will run broadcasts so if you want a cloned sprite to run a block of code just for that object you can run a custom block and only that object will run the code. So if you have 10 enemy clones and you want them to do something when they touch the player a custom block can be run when a clone detects the touch.
"Text (string) at speed of (string)
but can be used to organise your scripts or to use a script many times in your project. To use it, click “Create a Block” in the “My Blocks” section. Then, you can name your block. Then, click OK. Once it has been created, a “Define” block will appear. Under that block, place what you want the block to do. When you use the custom block, the code under the Define block will run.
Also, clones will run broadcasts so if you want a cloned sprite to run a block of code just for that object you can run a custom block and only that object will run the code. So if you have 10 enemy clones and you want them to do something when they touch the player a custom block can be run when a clone detects the touch.
- Srijani18
-
Scratcher
55 posts
How to use the custom blocks? When do we need to use it and why?
Okay! I understand now! This was really easy to understand! Thank you so much! 

- Srijani18
-
Scratcher
55 posts
How to use the custom blocks? When do we need to use it and why?
Thank you! I am clear with custom blocks now! 

- Srijani18
-
Scratcher
55 posts
How to use the custom blocks? When do we need to use it and why?
So this is how it's done! Thanks for the help! 

- ipaddude
-
Scratcher
100+ posts
How to use the custom blocks? When do we need to use it and why?
There's one more advantage that I haven't seen mentioned here:
Say someone made a complex script to do something. They could make a custom block (technically a subroutine) to reuse that code. However, there is one more advantage to that: other people can also reuse that code, without having to understand how it works. The only thing they have to know is the effect it will have based on their parameters. This is called abstraction.
Abstraction is really powerful so I suggest you read further on it.
For example, when you print a document from the internet, your browser interacts with your operating system which interacts with the printer driver. SInce every printer model works differently, there are different drivers you have to install to make the printer work. The drivers all have to provide a certain interface (usually standardized by the operating system) but they can communicate with the printer in different ways. In this way, all the details about how the specific printer model is dealt with by the driver, which provides an abstraction layer so that the operating system and your browser don't have to worry about how every specific printer works.
Say someone made a complex script to do something. They could make a custom block (technically a subroutine) to reuse that code. However, there is one more advantage to that: other people can also reuse that code, without having to understand how it works. The only thing they have to know is the effect it will have based on their parameters. This is called abstraction.
Abstraction is really powerful so I suggest you read further on it.
For example, when you print a document from the internet, your browser interacts with your operating system which interacts with the printer driver. SInce every printer model works differently, there are different drivers you have to install to make the printer work. The drivers all have to provide a certain interface (usually standardized by the operating system) but they can communicate with the printer in different ways. In this way, all the details about how the specific printer model is dealt with by the driver, which provides an abstraction layer so that the operating system and your browser don't have to worry about how every specific printer works.
- MrNanners
-
Scratcher
100+ posts
How to use the custom blocks? When do we need to use it and why?
Custom blocks have 2 main uses.
1. They can be used to do a set of code in 1 frame rather than multiple frames.
2. They can be used to save space and RAM by using less blocks, make things more organized, or be ease-of-access while in editor mode.
Hope this helped
1. They can be used to do a set of code in 1 frame rather than multiple frames.
repeat (30)this would normally take 30 frames (1 second, as Scratch runs at 30 FPS), but when you put it under a custom block and select the “run without screen refresh” box
change [x v] by (1)
end
define superSpeedthis will do this script in 1 frame, so nearly instantly. It is useful for checking entire lists or making rapid calculations in short periods of times.
repeat (30)
change [x v] by (1)
2. They can be used to save space and RAM by using less blocks, make things more organized, or be ease-of-access while in editor mode.
when I start as a clonenotice that if you replaced all of the “clone.anim” blocks under the “When I start as a clone” block with all the code under the “define clone.anim”, that a lot more space would be taken up, using more blocks, making it laggier, making it take longer to load your game, etc.
set y to [180]
forever
if <(important_stuff) = [good good]> then
clone.anim
end
if <(important_stuff) = [semi good]> then
clone.anim
end
if <(important_stuff) = [relatively okay]> then
clone.anim
end
end
define clone.anim
if <((y) mod (2)) = [0]> then
lots of
important code
change y by [-1]
else
even more
important code
change y by [-1]
end
Hope this helped

Last edited by MrNanners (Dec. 3, 2021 22:53:13)
- RL1123
-
Scratcher
1000+ posts
How to use the custom blocks? When do we need to use it and why?
The previous posts forgot about run without screen refresh.
This is an option to have the custom block finish in a single frame, which can make repeat loops finish in a flash. This helps with looping with lists and other things as the repeat loop is not instantaneous. You can't do this without a custom block.
This is an option to have the custom block finish in a single frame, which can make repeat loops finish in a flash. This helps with looping with lists and other things as the repeat loop is not instantaneous. You can't do this without a custom block.
Last edited by RL1123 (Dec. 3, 2021 22:55:33)
- 1492864
-
Scratcher
500+ posts
How to use the custom blocks? When do we need to use it and why?
When do we need to use it and why?Say you have this code:
change x by (10)you could simplify this code into:
turn cw (90) degrees
change x by (15)
turn cw (60) degrees
define change x (x) and turn (deg) degrees
change x by (x)
turn cw (deg) degrees
- JulianaH90
-
New Scratcher
1 post
How to use the custom blocks? When do we need to use it and why?
Hi! I´m trying to create a code in Scratch where I can click on different cities in a map, and the background will change.
I wanted to use a customized block to change the background, but as every city tag is a sprite, I can use a block I´ve created on another sprite/background.
Is there a way i can have my block available in other sprites?
Thank you!!
I wanted to use a customized block to change the background, but as every city tag is a sprite, I can use a block I´ve created on another sprite/background.
Is there a way i can have my block available in other sprites?
Thank you!!
- taskdomino
-
Scratcher
14 posts
How to use the custom blocks? When do we need to use it and why?
<<◯> and <◯>>its red so does it count as a custom block?
- DidntMeanToDoThat
-
Scratcher
500+ posts
How to use the custom blocks? When do we need to use it and why?
JulianaH90, I'm not entirely sure but I think the custom block will work on all sprites if you make it in a stage.
- DidntMeanToDoThat
-
Scratcher
500+ posts
How to use the custom blocks? When do we need to use it and why?
Oh nvm just tested, didn't work sry.
- Discussion Forums
- » Help with Scripts
-
» How to use the custom blocks? When do we need to use it and why?









