Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Mod Tutorial 1 - How To Add Blocks
- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
Hi, I'm dreamod and I've decided to start a series of Topics to help new modders learn more about squeak and how to make their own scratch mod.
This is my first one.
Note: This only applies to Scratch 1.4 and earlier.
First we'll discuss the basics of modifying Scratch. First download the source code here:http://info.scratch.mit.edu/Scratch_Source_Code_Licensed_Code . Open the .image file. Now shift click the file menu and exit user mode. Then you want to click the white background. You will see a menu. select “open…” and the browser.

A green window should show up. The browser is where we will be doing our programming. Select Scratch-Objects. This is a category containing Classes.
For Beginners In Programming
Classes are like a species of animals. Each class is different from one another and behaves differently. All classes (except Object) also have a superclass. (The opposite of a superclass is a subclass, basically a class the is “based off of that class”) A superclass also defines how that class behaves unless a method is overridden (you replace a features like walking with flying, for example). It's kind of like evolution, Everything derives from Bacteria (which can correspond to Object) and have critical essential features that come from it. They also have new features. For example an elephant has a trunk and a giraffe have a long neck. But they all come from the same Species/Class and behave more or less similar. This is a simple explanation of how classes work. You can read more about classes here Class (computer programming)
Instances are important for a class to work. When you create an instance, It's like creating a new individual giraffe. You can read more here Instance (Computer Science)
Now, carrying on…
Now select ScriptableScratchMorph (or if you want the block to be only for a sprite or stage select ScratchSpriteMorph or ScratchStageMorph respectively). Click on the class button. choose block specs and blockSpecs. You will see a bunch of code. This is a method. This tells ScriptableScratchMorph (or ScratchSpriteMorph or ScratchStageMorph) what to do when an instance (or class) sends that message to it.
You can read more about methods here: Method (Computer Science)
The code should look like this
I'm going to help you understand this code.
the first line is blockSpecs
This defines the name of the method.
The next line is | blocks |
This is how you create a variable for the method. You know what a variable is right? (further reading: Variables (Computer Science) )
This method is creating a variable named blocks. It is common to name variables in squeak “t” and a number. T stands for temporary. These variabes only work in that method and not the rest of the class or instance.
The next set of lines is very long. It's blocks _ #('control' ('wait %n secs' #t #wait:elapsed:from: 1 ETC…).
This means set the variable blocks to #('control' ('wait %n secs' #t #wait:elapsed:from: 1 ETC…).
This: #('control' ('wait %n secs' #t #wait:elapsed:from: 1 ETC…) is a list of all the block names, type, and selectors. The selector is used to call (ask the object to do and return the results of) a method which makes the block do something.
The block spec should look like this:
The - can be any of the following:
- stack block
b boolean reporter
c c-shaped block containing a sequence of commands (always special form)
r reporter
s special form command with its own evaluation rule
t timed command, like wait or glide
E message event hat
K key event hat
M mouse-click event hat
S start event hat
W when <condition> hat (obsolete).
If your block has paramaters then you can add default values after the selector. This is a parameter
Paramaters.
Paramaters in squeak look like this:
do: t1 with: t2 and: t3
your code here
and in blocks they would be written like this: #do:with:and:
When you call it directly you type *Your Object Here* do: *method or variable* with: *method or variable* and: *method or variable*
Now the last line, ^ blocks, self obsoleteBlockSpecs
this ^ means to return a value to the sender. Which is like what a reporter block reports.
Return Statement
The , means to join two lists, the variable blocks and a method: self obsoleteBlockSpecs This method returns the unused blocks.
Now that we have reviewed how this works I will explain how you add the block.
Find a place in the array (list) to put your block. Then you type your blockspecThe parentheses are very important. What they do is start a mini list inside a list. The blockspec is a list of things needed for the block to work. Then, that is in the big list.
Arrays
Try adding the block and see if it works. If it doesn't then please ask me for more specific help.
However, the block wont work yet, since it doesn't know what to do. You have to create a method. Click the instance button and type the name of your method. for example selector.
Then you just add your code. You will learn to do this if you observe some of the source code and learn a little about programming. I can help you with any questions.
try this code
I hope this helps somebody
This is my first one.
Note: This only applies to Scratch 1.4 and earlier.
First we'll discuss the basics of modifying Scratch. First download the source code here:http://info.scratch.mit.edu/Scratch_Source_Code_Licensed_Code . Open the .image file. Now shift click the file menu and exit user mode. Then you want to click the white background. You will see a menu. select “open…” and the browser.

A green window should show up. The browser is where we will be doing our programming. Select Scratch-Objects. This is a category containing Classes.
For Beginners In Programming
Classes are like a species of animals. Each class is different from one another and behaves differently. All classes (except Object) also have a superclass. (The opposite of a superclass is a subclass, basically a class the is “based off of that class”) A superclass also defines how that class behaves unless a method is overridden (you replace a features like walking with flying, for example). It's kind of like evolution, Everything derives from Bacteria (which can correspond to Object) and have critical essential features that come from it. They also have new features. For example an elephant has a trunk and a giraffe have a long neck. But they all come from the same Species/Class and behave more or less similar. This is a simple explanation of how classes work. You can read more about classes here Class (computer programming)
Instances are important for a class to work. When you create an instance, It's like creating a new individual giraffe. You can read more here Instance (Computer Science)
Now, carrying on…
Now select ScriptableScratchMorph (or if you want the block to be only for a sprite or stage select ScratchSpriteMorph or ScratchStageMorph respectively). Click on the class button. choose block specs and blockSpecs. You will see a bunch of code. This is a method. This tells ScriptableScratchMorph (or ScratchSpriteMorph or ScratchStageMorph) what to do when an instance (or class) sends that message to it.
You can read more about methods here: Method (Computer Science)
The code should look like this
blockSpecs
"Answer a collection of block specifications for the blocks that are common to all objects. Block specificatons (Arrays) are interspersed with category names (Strings). A block specification is an Array of the form: (<block spec string> <block type> <selector> [optional initial argument values]).
Explanation of flags:
- no flags
b boolean reporter
c c-shaped block containing a sequence of commands (always special form)
r reporter
s special form command with its own evaluation rule
t timed command, like wait or glide
E message event hat
K key event hat
M mouse-click event hat
S start event hat
W when <condition> hat (obsolete)"
| blocks |
blocks _ #(
'control'
('when %m clicked' S -)
('when %k key pressed' K -)
('when %m clicked' M -)
-
('wait %n secs' t wait:elapsed:from: 1)
-
('forever' c doForever)
('repeat %n' c doRepeat 10)
-
('broadcast %e' - broadcast:)
('broadcast %e and wait' s doBroadcastAndWait)
('when I receive %e' E -)
-
('forever if %b' c doForeverIf)
('if %b' c doIf)
('if %b' c doIfElse)
('wait until %b' s doWaitUntil)
('repeat until %b' c doUntil)
-
('stop script' s doReturn)
('stop all' - stopAll)
'operators'
('%n + %n' r + - -)
('%n - %n' r - - -)
('%n * %n' r * - -)
('%n / %n' r / - -)
-
('pick random %n to %n' r randomFrom:to: 1 10)
-
('%s < %s' b < '' '')
('%s = %s' b = '' '')
('%s > %s' b > '' '')
-
('%b and %b' b &)
('%b or %b' b |)
('not %b' b not)
-
('join %s %s' r concatenate:with: 'hello ' 'world')
('letter %n of %s' r letter:of: 1 'world')
('length of %s' r stringLength: 'world')
-
('%n mod %n' r \\ - -)
('round %n' r rounded -)
-
('%f of %n' r computeFunction:of: 'sqrt' 10)
'sound'
('play sound %S' - playSound:)
('play sound %S until done' s doPlaySoundAndWait)
('stop all sounds' - stopAllSounds)
-
('play drum %D for %n beats' t drum:duration:elapsed:from: 48 0.2)
('rest for %n beats' t rest:elapsed:from: 0.2)
-
('play note %N for %n beats' t noteOn:duration:elapsed:from: 60 0.5)
('set instrument to %I' - midiInstrument: 1)
-
('change volume by %n' - changeVolumeBy: -10)
('set volume to %n%' - setVolumeTo: 100)
('volume' r volume)
-
('change tempo by %n' - changeTempoBy: 20)
('set tempo to %n bpm' - setTempoTo: 60)
('tempo' r tempo)
'motor'
('motor on for %n secs' t motorOnFor:elapsed:from: 1)
('motor on' - allMotorsOn)
('motor off' - allMotorsOff)
('motor power %n' - startMotorPower: 100)
('motor direction %W' - setMotorDirection: 'this way')
'variables'
('show variable %v' - showVariable:)
('hide variable %v' - hideVariable:)
'list'
('add %s to %L' - append:toList: 'thing')
-
('delete %y of %L' - deleteLine:ofList: 1)
('insert %s at %i of %L' - insert:at:ofList: 'thing' 1)
('replace item %i of %L with %s' - setLine:ofList:to: 1 'list' 'thing')
-
('item %i of %L' r getLine:ofList: 1)
('length of %L' r lineCountOfList:)
('%L contains %s' b list:contains: 'list' 'thing')
).
^ blocks, self obsoleteBlockSpecs
the first line is blockSpecs
This defines the name of the method.
The next line is | blocks |
This is how you create a variable for the method. You know what a variable is right? (further reading: Variables (Computer Science) )
This method is creating a variable named blocks. It is common to name variables in squeak “t” and a number. T stands for temporary. These variabes only work in that method and not the rest of the class or instance.
The next set of lines is very long. It's blocks _ #('control' ('wait %n secs' #t #wait:elapsed:from: 1 ETC…).
This means set the variable blocks to #('control' ('wait %n secs' #t #wait:elapsed:from: 1 ETC…).
This: #('control' ('wait %n secs' #t #wait:elapsed:from: 1 ETC…) is a list of all the block names, type, and selectors. The selector is used to call (ask the object to do and return the results of) a method which makes the block do something.
The block spec should look like this:
('block name' - blockSelector)- stack block
b boolean reporter
c c-shaped block containing a sequence of commands (always special form)
r reporter
s special form command with its own evaluation rule
t timed command, like wait or glide
E message event hat
K key event hat
M mouse-click event hat
S start event hat
W when <condition> hat (obsolete).
If your block has paramaters then you can add default values after the selector. This is a parameter

Paramaters.
Paramaters in squeak look like this:
do: t1 with: t2 and: t3
your code here
and in blocks they would be written like this: #do:with:and:
When you call it directly you type *Your Object Here* do: *method or variable* with: *method or variable* and: *method or variable*
Now the last line, ^ blocks, self obsoleteBlockSpecs
this ^ means to return a value to the sender. Which is like what a reporter block reports.
Return Statement
The , means to join two lists, the variable blocks and a method: self obsoleteBlockSpecs This method returns the unused blocks.
Now that we have reviewed how this works I will explain how you add the block.
Find a place in the array (list) to put your block. Then you type your blockspec
('name' - selector)Arrays
Try adding the block and see if it works. If it doesn't then please ask me for more specific help.
However, the block wont work yet, since it doesn't know what to do. You have to create a method. Click the instance button and type the name of your method. for example selector.
Then you just add your code. You will learn to do this if you observe some of the source code and learn a little about programming. I can help you with any questions.
try this code
selector
DialogBoxMorph inform: 'Yay! I've made a block!'.
^ ':)'
I hope this helps somebody

Last edited by dreamod (Oct. 27, 2013 07:08:48)
- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
Good tutorial.Thanks. I'm going to make some other tutorials too, like how to change the title, add categories, add args, and c blocks and more.
- nathanprocks
-
Scratcher
1000+ posts
Mod Tutorial 1 - How To Add Blocks
C BLOCKS!!!!! I have always wanted a tutorial for that!!Good tutorial.Thanks. I'm going to make some other tutorials too, like how to change the title, add categories, add args, and c blocks and more.

- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
That'll come a little later. I don't completely understand them yet.C BLOCKS!!!!! I have always wanted a tutorial for that!!Good tutorial.Thanks. I'm going to make some other tutorials too, like how to change the title, add categories, add args, and c blocks and more.
- DigiTechs
-
Scratcher
500+ posts
Mod Tutorial 1 - How To Add Blocks
Great tutorial. I'd like to find out how to make C blocks or E blocks, if you don't mind 
Well, I could find out myself. I know where to look, which is good.

Well, I could find out myself. I know where to look, which is good.
- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
Great tutorial. I'd like to find out how to make C blocks or E blocks, if you don't mind
Well, I could find out myself. I know where to look, which is good.
Sure thing. I'll write a tutorial when I find time. I'm not too much of an expert myself but I've made a few of them
Last edited by dreamod (June 18, 2014 18:47:44)
- Kal_Kal_Kal
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
Wait, How do you create a method?
- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
Wait, How do you create a method?Just type it in a class and accept.
- EMPedemonte20
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
When in the second tutorial coming out?
- EMPedemonte20
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
Wouldn't that be special formThat'll come a little later. I don't completely understand them yet.C BLOCKS!!!!! I have always wanted a tutorial for that!!Good tutorial.Thanks. I'm going to make some other tutorials too, like how to change the title, add categories, add args, and c blocks and more.
- Kal_Kal_Kal
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
The Source link just redirected me to HelpYeah, is it the same using ShiftClickR?
Is the Source Code the same as downloading a normal 1.4 or is it different?
- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
The Source link just redirected me to HelpYeah… things have changed since 2.0 You can still download it from the Wiki Article
Is the Source Code the same as downloading a normal 1.4 or is it different?
- Rumanti
-
Scratcher
1000+ posts
Mod Tutorial 1 - How To Add Blocks
Bookmarked, thanks. I'll look into that this weekend.The Source link just redirected me to HelpYeah… things have changed since 2.0 You can still download it from the Wiki Article
Is the Source Code the same as downloading a normal 1.4 or is it different?
- cardwellkatie91
-
Scratcher
53 posts
Mod Tutorial 1 - How To Add Blocks
i want a modding tutarial for scratch 1.4 30-jun-09 beacose this dosent work in that 
EDIT:i found blockspecs

EDIT:i found blockspecs
Last edited by cardwellkatie91 (Aug. 26, 2016 17:18:38)
- dreamod
-
Scratcher
100+ posts
Mod Tutorial 1 - How To Add Blocks
i want a modding tutarial for scratch 1.4 30-jun-09 beacose this dosent work in thatAnything in particular?
EDIT:i found blockspecs
- Discussion Forums
- » Advanced Topics
-
» Mod Tutorial 1 - How To Add Blocks
















