Discuss Scratch

Nuclear_Melon
Scratcher
100+ posts

How do custom blocks work?

Why do so many people use custom blocks?
Basically I want some reasons/scenarios where it is good to use them.
Also, I don't know how they work. That too.
historical_supa
Scratcher
1000+ posts

How do custom blocks work?

There is an option to make them not use screen refresh and makes the script run fast. It also reduces the file space according the wiki. Good to use in laggy projects and advanced projects.
-fallingstars--
Scratcher
5 posts

How do custom blocks work?

Hello! This is -fallingstars– answering the question “Why do so many people use custom blocks?” from Nuclear_Melon.
—————————————–
Custom blocks are blocks where you can make a separate script that runs itself any time it is encountered in the script. These blocks look very similar to broadcast messages at first, but there is one major difference that sets them apart. Custom blocks allow you to include inputs which can easily reduce extra lag. For example:

This is how it would be if I DIDN'T use a custom block:

set [if I want just a variable] to [70]
broadcast [and a broadcast block...]



when I receive [and a broadcast block...]
repeat (if I want just a variable)

end

If I HAD used a custom block, the first script part would be combined into one and the other one would be the exact same length. All I would need to do is delete the variable block, put the 70 into an input spot instead and swap out all the variables for the input oval that comes with the define block. (It's a lot easier to explain when the forum scratch blocks are up to date and easy to work with.) With a custom block you can add multiple inputs and they can be either boolean or number or text. You can also add multiple labels. Another benefit of the custom block is that it has a “run without screen refresh” checkbox, which is useful for things such as platformers.


So where do you find these beauties? You find these blocks in the “My Blocks” section of the selection of blocks, next to the variables. It's pretty often overlooked and it's a shame, especially considering what a time and lag saver it is. If you want, I could make a project on how to use custom blocks and share the link here.
deck26
Scratcher
1000+ posts

How do custom blocks work?

Simple example. Say you had a custom block that found the smallest of 3 values in the variable ‘result’. Then

define smallest (a) (b) (c)

smallest (x) (y) (z)
set [smallest-val v] to (result)
smallest (r) (s) (result)
set [smallest v] to (result)
would set smallest to the smallest value from r,s,x,y and z. To do that with a broadcast would mean setting the 3 parameters before calling the broadcast.

Also custom blocks are only run for the calling object whereas if the sprite is cloned all the clones will also respond to a broadcast. So they're great for creating a spearate code block for clones.

Or you're drawing lots of circles

define draw-circle (x) (y) (radius)
code to draw circle
Then you can just have a simple custom block call to draw a circle anywhere on screen of any size. Add in the no screen refresh option to that and it's quite powerful. Try doing something like https://scratch.mit.edu/projects/55182496/ without that.
APB15
Scratcher
1 post

How do custom blocks work?

h
Malicondi
Scratcher
1000+ posts

How do custom blocks work?

APB15 wrote:

h
please don't necropost or post unnecessary posts.

Powered by DjangoBB