This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-03-01 20:13:08

ScratchReallyROCKS
Scratcher
Registered: 2009-04-22
Posts: 1000+

Programming in Squeak

This should help new modders/Squeakers start to program in Squeak Smalltalk. This tutorial will NOT explain how to apply it's contents into modifying Scratch. For this, I would suggest this great tutorial by Pecola1.

At one point, I too was just someone who was struggling with getting a grasp on programming in Squeak. I hope to explain, in terms that beginners can understand, what I have gathered through research and taught myself.

Section 1: Messages
Like all object oriented programming languages, programs made in Squeak make things happen by passing messages between objects. The syntax for this is as follows:

Code:

<object> <message>

It's as simple as a space between the object and the message. Now, using this syntax, here's an example of how I would tell the 'Square' object to draw a 10 by 10 square on the screen:

Code:

Square makeSquare: 10.

This simply tells the Square object to invoke the 'makeSquare:' method with an input (called an 'argument') of '10'. Now, you may be asking "Where did the makeSquare method come from?". Well, it's a method contained in the 'Square' class. I'll explain classes and methods later.

Section 2: variables
Part 1: declaration
Variables in Squeak can be declared in a number of ways. They can be declared as class variables, instance variables, local script variables, or global variables. For the purposes of this section, we will be mainly focusing on local script variables.

Declaring a script variable is done like this:

Code:

| <variable name> <other variable name> <etc.> |

So, if I wanted my script to contain the variables 'foo' and 'bar', I would use this:

Code:

| foo bar |

These variables can ONLY be used in the script they are written into. As far as I know, script variables must be the first line in a code.

Part 2: values
There are two different things a variable can be set to; a reference to an external object or method, or a direct value fed into it (string, array, number). To set a variable to an object or method, use this:

Code:

<variable> := <object/method>

So, if I wanted to create a variable called 's' and have it represent that 'Square' object I mentioned before, I would use this code:

Code:

| s |
s := Square.

The variable 's' is now a reference to the Square object, meaning that any messages I send to it would actually be sent to the Square object.

To set a variable to a direct value, use this:

Code:

variable _ <value>

Using this, say I wanted to add to my previous script to make it actually draw a square of varying dimensions:

Code:

| s width |
s := Square.
width _ 20.
s makeSquare: width

Section 3: Classes and Instances
Part 1: What are classes and instances?
If you already know about this topic, just skip to the syntax section. The concept of classes and instances is, in my opinion, the most complicated and confusing part of object oriented programming. Once you know it, it can be easily applied to Squeak though.

I found it very hard, as a beginner, to grasp this concept. I will try as hard as I can to explain it as well as I can.

Essentially, a class is the blueprint for all of it's instances. So, the model car that the car designers make is the class for all the cars that will be made off of it. Classes contain all the scripts and variables that every instance will carry. They also contain their's own scripts and variable for use in the class only.

An instance is a copy of that class, and, although they all come from the same blueprint, every instance can be different. This is where instance variables come into play. Say I told the 'Square' class to make two new 'Square' instances, and then changed the 'color' instance variable on one of the instances from 'Color red' to 'Color blue', and then told both instances to make squares using the makeSquare: method. Then, I would have a red square and a blue square, even though they are both from the same class.

Part 2: Using them in Squeak
To create a new instance of a class in Squeak, you need to set a variable to an instance object of that class:

Code:

<var> := <class> new.

If you remember from above, this means that I'm sending the message 'new' to the class object '<class>'. Now, the method 'new' doesn't actually have to be defined in the class. The reason for this, if you want to get really technical, (I think) is that 'class' objects are subclasses of a Squeak superclass which has 'new' defined. (I don't happen to know what that superclass is)

Also, instance methods and variables must be in the 'instance' section of the class.
___________

To make a class in Squeak, open up a system browser, click on a category on the far left side, and the scripting panel should say:

Object subclass: #NameOfClass
    instanceVariableNames: 'instVarName1 instVarName2'
    classVariableNames: 'ClassVarName1 ClassVarName2'
    poolDictionaries: ''
    category: '<category-name>'

Replace where it says 'NameOfClass' with the name of the class you want to make, then fill in the class variables and instance variables with their respective names. Once you've saved this, you can work with your new class.

Part 3: Why would these be useful?
Making instances of a class is very useful in many programs. If you are making a shooting game, then you may want to make a 'bullet' class so that you can make instances off of it every time the gun is shot. This makes it so that multiple bullets could be fired at the same time, and they all could have a unique flight path and/or color.

Section 4: Syntax reference
Here's a reference most of the syntax of Squeak Smalltalk:

<object> <message> -- sends a message to the specified object
| <var name> | -- creates script variables
<var> _ <value> -- sets a variable to a specific value
<var> := <object/method> --sets a variable to a reference to the specified object/method
[ <statements> ] -- evaluates the code in the brackets and uses them as an input to a method
[ :<var> | <statements> ] -- evaluates code inside brackets with a local variable
#(<value 1> <value 2>) -- creates an array (list) of values
<statement>. -- ends a line of code
^ <value> -- returns a value and stops the method.

________________________________________________
I will be adding to this

Thanks for reading, I worked hard on this.

Last edited by ScratchReallyROCKS (2011-03-01 20:13:35)


http://imageshack.us/a/img694/3806/sigmad.png

Offline

 

#2 2011-03-01 21:51:00

hpotter134
Scratcher
Registered: 2010-02-21
Posts: 100+

Re: Programming in Squeak

Great intro tutorial! But could you please clarify:

ScratchReallyROCKS wrote:

There are two different things a variable can be set to; a reference to an external object or method, or a direct value fed into it (string, array, number). To set a variable to an object or method, use this:

Code:

<variable> := <object/method>

To set a variable to a direct value, use this:

Code:

variable _ <value>

What is the difference between := and _ ? I thought they were interchangeable?


http://i45.tinypic.com/fxgtvc.png

Offline

 

#3 2011-03-02 06:38:21

ScratchReallyROCKS
Scratcher
Registered: 2009-04-22
Posts: 1000+

Re: Programming in Squeak

hpotter134 wrote:

Great intro tutorial! But could you please clarify:

ScratchReallyROCKS wrote:

There are two different things a variable can be set to; a reference to an external object or method, or a direct value fed into it (string, array, number). To set a variable to an object or method, use this:

Code:

<variable> := <object/method>

To set a variable to a direct value, use this:

Code:

variable _ <value>

What is the difference between := and _ ? I thought they were interchangeable?

:= sets a variable to a reference to an external method or object, whereas _ sets a variable to a direct value.


http://imageshack.us/a/img694/3806/sigmad.png

Offline

 

#4 2011-03-02 08:22:21

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Programming in Squeak

ScratchReallyROCKS wrote:

:= sets a variable to a reference to an external method or object, whereas _ sets a variable to a direct value.

Not quite. Both symbols are equivalent and assign a value to a variable. The underscore character transforms into a left-arrow in Squeak, because that's what it was way back in Xerox PARC on the Alto, the first Smalltalk machine. The := message is the ANSI standard for all Smalltalks.


Jens Mönig

Offline

 

#5 2011-03-02 15:29:31

ScratchReallyROCKS
Scratcher
Registered: 2009-04-22
Posts: 1000+

Re: Programming in Squeak

Jens wrote:

ScratchReallyROCKS wrote:

:= sets a variable to a reference to an external method or object, whereas _ sets a variable to a direct value.

Not quite. Both symbols are equivalent and assign a value to a variable. The underscore character transforms into a left-arrow in Squeak, because that's what it was way back in Xerox PARC on the Alto, the first Smalltalk machine. The := message is the ANSI standard for all Smalltalks.

Oh. Well, I guess I've just seen it used that way a lot. Good to know  smile


http://imageshack.us/a/img694/3806/sigmad.png

Offline

 

Board footer