View this PageEdit this PageUploads to this PageVersions of this PageHomeRecent ChangesSearchHelp Guide

Simple Scripting

Here we cover the very basics or writing scripts for objects. Important ideas such as using tests (conditional statements) and using user-defined variables are introduced in a later section.

The goals for this lesson are to learn to
  • identify the icons in the scriptor heading, and their uses
  • change the tick rate for a script
  • substitute random numbers in the place of regular numbers in a script
  • change the condition under which a script runs
  • program a script to change the value of properties of objects
  • understand the difference between "fire-once" scripts, and "ticker" scripts, and the difference between "normal", "ticking", and "paused" states of scripts and how these determine the effect of the scripting stop/go button found in the supplies bin.
  • explain the basic conceptual model of the Squeak "Script Boss" (our name) who determines exactly what happens at each moment.

You've already done some simple scripting, with the car project.
You know that in the viewer, scripts are those lines that begin with a yellow exlamation mark Uploaded Image: exclamation.gif.
If you drag any one of these (like "Claire forward by 5") out of the viewer and set it down on the world, it will sprout a scripting box around it:
Uploaded Image: forwardby5script.gif
This box is called a scriptor. You can then drag in other scripting commands to make a single larger script with several statements in it. Note We do not like scripts named "script1". We always change scripts to names that explain what they do. Similarly, we always change the names of objects we create to something meaningful, not "sketch12". This avoids a lot of confusion when you have multiple scripts and multiple objects.

SCRIPTOR HEADER
Look at the first line of the script box above (with "Claire script1" in it).
The first line is called the script header.
Let's examine what is available in the script header for any scriptor.

Uploaded Image: exclamation.gif Just like in the viewer, click on this and the script ones once.
Uploaded Image: closecircle.gif If you click here, the script will disappear, but not be destroyed. You can obtain it again by dragging it from the viewer.
Uploaded Image: codebox.gif reveals smalltalk code that corresponds to the script. fun to look at, but don't go there as part of this course.
Next comes a box with the name of the scriptee (Claire, in this case)
Then a box with the name of the script itself (script1 here ... a bad name)
A small clock called the tick indicator Uploaded Image: TickIndicator.gif. If you click on it, the script will start "ticking", and Claire will begin to move 5 steps at a time over and over. How many "ticks" occur per second? This can be adjusted by clicking on the tick indicator and holding down the mouse, to obtain the following menu Uploaded Image: ticks.gif.
Next comes the small phrase that indicates under what conditions the script executes, or how the script is triggered.
Uploaded Image: trigger.gif
If you hold down the mouse over this, you will get the following menu of options
Uploaded Image: whenshouldrun.gif
Next is a small box
Uploaded Image: tearofftest.gif which we describe in the next chapter.
Finally, BE CAREFUL, the "destroyer" button Uploaded Image: destroyer.gif will remove your script entirely, and it will be gone. (Not just hidden from the desktop.



Explorations


Ticking Clock
  • Draw a circle (hold down shift to make it perfectly circular) using paint. Keep it, close the paint window, and name it "clock".
  • Open up paint again, select the line drawing tool, and hold down shift to draw a perfectly straight vertical line, of length roughly half the diameter for your clock. Save it, and name it "hand".
  • Position the hand so that the bottom is at the center of the clock, and the top of the hand is at the "12 oclock" position.
  • Open a viewer for "hand", and drag out "hand turn by 5", which will sprout a script box around it. Click on "script1" in the title bar, and change it to "tick", so that you have the following:
Uploaded Image: handturnby5.gif
  • Start it ticking by clicking on the clock icon in the script header.
  • What is wrong? The hand is rotating around its center. We don't want the hand to spin, but to turn from one end. We need to change the center of rotation which is shown by the small blue circle with crosshairs that appears on the object when the halo is present. Hold down shift, and drag this to the end of the hand opposite the direction of the arrow.
  • Now start it ticking again.

TICK RATE
We can change the tick rate of the clock, and have it accurately mark off the seconds.
Hold down the mouse button over the small clock icon, and set the tick rate to 1
Uploaded Image: ticks.gif
Now, how many degrees (out of 360 = once around) does the second hand move on an actual clock? Well, it moves 360/60 = 6 degrees each second. Change the "5" to say "6" by clicking on the green up arrow.
The clock should now be ticking off seconds.

Experiment with the ticks/second and have it tick at a different rate (try 100/sec).
every script can have its speed adjusted in "ticks per second",
The number of ticks/second can be a fraction. Type in "0.1" after selecting "other" in the menu. The hand will move once every 10 seconds. (You need to type 0.1, not .1. And remember to hit the return key, or click on "accept".)

Change the ticks/second back to 1.

TURN BY ANY AMOUNT AT EACH TICK
How can you make the clock move backwards? Replace "turn by 6" with "turn by -6".


RANDOM NUMBERS
If we'd like the clock to behave erratically, we can have it turn a random amount at each step. When you position the mouse over the word "hand" in the header of the script, the cursor will turn into a small menu. Click and hold down, and select "hand me a random number tile", then drag that and put it in place of the "6" in the script:
Uploaded Image: randomtick.gif
At each tick, a number is chosen between 1 and 180 randomly, and that is the amount that the hand moves. Change "180" to "30".
An alternative place to find the random number tile is from the supplies bin.

CHANGING WHEN IT RUNS
Click on the small clock icon to stop the script from ticking.
Hold down the mouse over the word "paused" in the script header, and select "mouseDown" from the menu that appears.
Uploaded Image: whenshouldrun.gif
Now the hand will move only when you click the mouse down on it. Try it.
Change the condition to read "mouseStillDown", then click on the hand and hold the button down. The hand should continue to turn until you release the mouse button.
Experiment with other conditions. For help, select "what do these mean?" from the menu.

The three main settings are "normal", "paused", or "ticking".

A "normal" script is usually meant to be executed once through (for example, by clicking on the yellow exclamation mark), and not meant to be set to "ticking" where the statements will be run over and over again. This does not mean though that during the entire run of a project a "normal" script won't execute more than once. An example is a script that resets objects to particular locations. You may want this to happen from time to time (e.g., pac-man returns to the center), but it would not make sense to have such a script continuously ticking (because, for example, pac-man would be unable to move away from the center).

A "ticking" script is one whose statements are run through over and over. All statements are executed in order from top to bottom, and after the last statement in the script is run, they all are begun again, starting from the top.

A "paused" script is a "ticking" script that is not currently running. A script is never paused between two statements in the middle of the script.... if the first statement in a script is run, then they all are, and the "pause" will take effect after the last statement in the script is next executed. (Programmers will be interested to know that scripts are "atomic", and each ticking script is executed completely once through without interruption from any other ticking script. )

So, what is the difference between "paused" and "normal"? None, really, except for the very useful tool labeled "All Scripts" found in the supplies bin. When the "go" button is pushed, all paused scripts are started running again, and when the "stop" button is pushed, all ticking scripts are then paused. Scripts set to normal are not started ticking (though there is an option to make this happen.)

We refer to scripts typically intended to be run over and over again as tickers, and other scripts as fire-once scripts. So far, you have mostly dealt with tickers.





The following need to be made user-friendly and graphical

Random line drawing by putting pen down, then fwd random 180, turn random 180.
Have several all going, with different colors.

Your own drawing tool.
  • drag an ellipse out of the supplies bin
  • set border to 0 (on the color and borde pane)
  • drag out "stamp" found on the miscellaneous pane, to get a new script
  • set it ticking
  • drag it around with the mouse, and watch what happens.

Create a script that does the following:
  • Claire fwd random 200
  • Claire turn by random 360
  • Claire stamp


Butterfly on arcs
  • Draw a butterfly
  • Create a script that does:
    • stamp
    • forward by 40 (or some other decent sized number
    • turn by (random 5/10/20)
    • bounce silent


Geometry art
Set Claire's penDown to true in the viewer.
Claire geometryart
  • Claire forward by 100
  • Claire turn by 90
  • Claire forward by 100
  • Claire turn by 90
  • Claire forward by 100
  • Claire turn by 90
  • Claire forward by 100
  • Claire turn by 90
  • Claire turn by 5

Now start it ticking.



Make a slider work like a joystick.
  • drag out a slider from the supplies bin, and open up a viewer, and the special "slider" pane
  • notice maxval, minval, current val
  • set maxval to 90, minval to -90
  • rename the slider to "angle"
  • rotate it so the top is now on the right.
  • create a new script for Claire, containing "forward by 5"
  • add to it Claire turn by 5
  • replace "5" from turn by slider's currentval
  • set it ticking, and use the slider to control direction.
  • bonus: add another slider, call it "distance", and set its max and min vals to, say, 1 and 100, and then replace the fwd by amount with its current val. You now have two sliders that control the motion of Claire.


Making objects chase
  • draw a new character Colin, save, open viewer, look at the "motion" pane.
  • drag out "turn toward dot", sprouting a new script.
  • name the script "chase"
  • we want to replace "dot" with a tile that says "Claire".
  • get that tile in either of two ways:
    • halo of Claire... orange icon
    • viewerheader for claire - menu icon... get "tile representing me"
  • add "forward by 5" line to the script.
  • now set it ticking.
  • drag Claire around and see what happens.
Uploaded Image: chase.gif
How can we Colin follow Claire in a less direct fashion?
We can simply modify the direction of Colin by a reasonably large random amount:
Uploaded Image: randomchase.gif
Why does this work?
What would happen if we switched the positions of the "turn by" and the "forward" statements like this:
Uploaded Image: badrandomchase.gif


This is a good time to talk about stepping through your script to understand exactly how it works. Pretend to be the computer and do one step at a time, from top to bottom. Does that help explain the difference between the two scripts?

This simple behavior forms the beginnings of a pursuit game. We could control Claire with a joystick, and have Colin chase her. But how do we control what happens when Claire gets caught? We'll need Tests which we'll see in the next section.



RECAP
lines with excl. mark are built-in scripts.
drag them out, get script box
can add other scripting lines to get compound script, executes one line after another.




Can we change the pen thickness while it is going?
We can by hand, but how can we get the program to do it?

Exercises with Assignment statements.
Explain green/purple arrow.

New randomart, with line thickness set to random.

Jumping claire
set x, y random when mouse clicks
Following claire
set x, y to track mouse (off by 100, so don't get stuck)

Make Claire hide
  • Draw giraffe body with hole for head, on a "board"
  • set x, y to go hide there.
  • send to back (by hand)

Sending to a place (executed once) is a useful thing for the beginning of a game,
or when a ball needs to be served, or to reposition things.


Recap
  • make scripts by putting tiles together
  • run once or over and over
  • each line executes a built-in script (forward, turn, stamp) OR changes an assignment
  • difference between dragging out by green arrow, or by value.
  • tiles are like nouns, replace similar things. a number property can go wherever a number is, etc.
  • expand/contract math arrows.
  • random number tiles obtained. (cant be expanded, so need to make add'l places ahead of time if you want to modify the value by an offset)
  • name of object tile obtained




Old notes
  • Squeak has some predefined actions (methods, or built-in-scripts) such as foward, make sound, etc. (include explorations using predefined actions only, on a single object).
  • You can create your own scripts by composing one or more predefined actions together. These will execute one after another.
  • A script can be made to run through exactly once.
  • Or they can go over and over
  • It is possible to control how fast, or how many times through per "tick".
  • A single object can have multiple scripts controlling it.
  • A single script can control multiple objects
  • Interesting things can be done with multiple objects and multiple scripts
  • Objects can be made to relate to eachother via drag/drop tiles. (still no variable assignment)
  • Scripting practice while exploring panes.
  • Scripting practice while using objects from supplies bin.
  • Math functions, triangles expanding and contracting
  • Tile composition
  • Assignment statements
    • we can change a property of an object manually
    • a script can change a property of an object while it is running.
    • a property can only take on the type of values that correspond to its data type
    • explorations and fun excursions using various data types and assignment statements under program control.
    • the arrow operator


Link to this Page