Sunday, September 19, 2010

When do scripts Run in Etoys?

With Etoys, children can draw their own sketches then bring them to life by writing "scripts" that tell the sketches what to do.  This post will explain how you can specify when scripts should run.


After watching the video, see if you can figure out what "MouseDown" and "MouseStillDown" do.  Play around and run some tests to figure it out.

You can also learn about scripting by reading excerpts from the soon to be released Etoys Manual or just playing around and running some tests.


Excerpts from soon to be release Etoys Manual

3.6 Script Editor

joystick_script.jpg 
 
 
Although some object actions can be controlled in the object's viewer, the script editor is where you assemble the tiles to create more complex actions or scripts. In the script above two of the object sketch's tiles have been dragged from sketch's viewer and dropped into the Script Editor. When the tile is in place to be accepted by the Script Editor, the area around the tile turns green and when the tile is released an audible click is heard if sound is enabled.
The Script Editor allows you to assemble tiles and test out your ideas for creating a simulation in the Etoys world. Getting into the habit of testing your ideas is a very powerful concept. You can use the results of these test scripts to simulate and determine the attributes and behaviors of the objects in your world.  Getting into the habit of testing your ideas or at least thinking about how you could test your ideas (and what other people tell you is true) is a wonderful "habit of the mind". Let your imagination reign.

Collapse button
The round brown button with the black enclosed circle at the upper left collapses the script back to the viewer. The script can always be viewed in the Script Editor again by dragging it out onto the world from the object's viewer.
Name of the Player being Scripted.
The text to the right of the collapse button is the name of the object for which you are creating the script. You can not change the objects name here.

Name of script.
The text to the right of the object's name is the script's name. It's a good idea to give a good name to the script (e.g. If its a script that moves the player, you can call the script 'move', etc.).
Exclamation point.

Graph-clear.png

The yellow exclamation point in the center of the Script Editor runs the script one time. You can also run the script from the object's viewer as shown above.

Tickindicator
The TickIndicator is the clock face to the right of the yellow exclamation point. The TickIndicator is light green when not in use, pink when script is paused and blue when script is ticking. Hold mouse button down and a menu pops up. There you can set the rate at which the script should run. The default is 8 ticks a second. Predefined in the menu are choices from one tick to 100 ticks a second. Last menu option is called other... Here you can type in the number of ticks per second that you want the script to run. A tick rate of 0.5 indicates one tick every other second, 0.1 once every 10 seconds, etc.
ScriptStatusControl
This appears to the right of the clock. Click it to get the following menu of options:

  • normal - run when called
  • paused - ready to run all the time, will change to "ticking" Status when you press the "Go" button in the "All Scripts" object.
  • ticking - run all the time, will change to "paused" Status when you press "Stop" button in the "All Scripts" object
  • mouseDown - run once when mouse goes down the object
  • mouseStillDown - run while mouse still down on the object
  • mouseUp - run once when mouse comes back up off the object
  • mouseEnter - run once when mouse enters the object's bounds with the button up
  • mouseLeave - run once when mouse exits the object's bounds with the button up
  • mouseEnterDragging - run once when mouse enters the object's bounds while dragging another object
  • mouseLeaveDragging - run once when mouse exits the object's bounds while dragging another object
  • opening - run once when the object is being opened, this only works for World and Pages (contained in a book) objects.
    Pop Quiz: Can you figure out how to use this to ensure a Book always opens to the first page when a project is opened? Can you figure out how to use this to reset a Page each time it is opened?
  • closing - run once when the object is being closed. This only applies to World and Pages objects. 
More events:

  • connectedTo - run once immediately after a connector has connected to the object
  • disconnectedFrom - run once immediately before a connector is going to disconnect from the object
  • KeyStroke - executed when any key is pressed, this is only available from World and Playfield scripts 
  • acceptedTextContents - executed when text is accepted, which can be when you tab to the next text field or hit return (if accept on CR is checked), this is only available for Enhanced Text objects.


Gold chest.
Clicking on the gold chest to the right of the ScriptStatusControl allows access to the following special tiles:
  • test tile
    This tile has three holders into which you can place one or more tiles:
    test.png
    Test: You can place any tile that contains an attribute of any object. For example, you could test whether Ellipse's y value is less than 5 as shown. To accomplish this grab the left side of the Ellipse's y tile from Ellipse's viewer. Make sure you aren't to the right of the tile and have the red box surrounding Ellipse's y and its value at the right, because then you will obtain an assignment phrase for the tile and not its attribute. You can test on almost any attribute (numeric, color, graphic, etc) of any object.
    Pop Quiz: How could I define a test to see if a car passed a finish line in a race?
    Yes: Tiles placed in the "Yes" holder will be executed when the "Test" is true. If y<5, then Ellipse will turn blue.

    No: Tiles placed in the "No" holder will be executed when the "Test" is false. If y>=5, then Ellipse will turn blue.
    Note, that many tiles can be used in the "Test" holder of the "Test Yes No" tile. Some can also be used as watchers, which can be useful when debugging your scripts. While you may think a "Sketch obtrudes" or a "Sketch is under mouse" the Etoys system may think differently.  Being able to see this as a script runs can help.
  • repeat tile
    This tile has one holder called "do" into which you can place a script:
    Repeat.png
    You can repeat the script you drop into the "do" placeholder the number of times set in the number box at the top of the tile.


  • random tile
    Random.png
    The random number will range from 0 to the number in the number box (in this case 5). It can be dropped into the end of a command that requires a number or function. This is a special case of a function tile.
  • function tile
    Function.png

    You can choose a function by clicking on the arrows at the left or clicking on the function name (in this case "abs") to get a drop down list of functions to choose from. Most of the common functions are available in the list. The function can be dropped into the end of a command that requires a number or function.
  • button up?

    ButtonUp.png Used in Test and its value is true when the mouse is NOT pressed down.

  • button down?


    ButtonDown.png
    Used in Test and its value is true when the mouse is pressed down anywhere.

  • tile representing the player 
    Ellipse.png
    In this case the player is Ellipse.

  • number
    Number.png
    A number box that can be dropped into the end of a command that requires a number or function.

 
Script editor menu
The Script Editor Menu button appears on the far right. It has the following options.
add parameter

This option adds a parameter to the script.  The parameter is passed into the script when it is called from another script. For example if you can create a script that will move an object forward based upon the number, you can add a parameter of type number. Then call that script from one or more other scripts passing in a specific value as the number. To use the parameter in the script simply drag the parameter from the Script Editor's in the top line to the right of the scripts name into your script where any number data type can be used.
There following are the data types that can be used as parameters:
  • Boolean (true or false)
  • BorderStyle
  • ButtonPhase
  • Color
  • Graphic
  • ImageResolution
  • Number
  • Patch
  • Point
  • ScriptName
  • Sound
  • String
  • TrailStyle
button to fire this script This will create a button object that can be used to fire the script. You can change the button's label from the menu in its viewer by selecting change label.
show arrows
When selected the arrows inside scripting tiles will be displayed (ex: the up and down arrows to increase numbers and the left and right arrows to remove and parts of the expression
edit balloon help for this script
This edits the balloon help that is shown when you hover over the script name in the scripts category of the Objects Viewer

explain status alternatives
Opens a window with descriptions of the options for "when this script should run" (ex: normal et al)

show code textually Will provide a text version of the script which you can edit. This provides an entry into Squeak programming.  To save your changes type
show code textually Will provide a text version of the script which you can edit. This provides an entry into Squeak programming.  To save your changes type <ctrl><s> (on PC and XO) <cmd><s> (on Macintosh) when done.  Note once changes are made you can not revert back to tile scripting without losing your changes.
save this version When editing the text version of a script you can use this menu option to save the changes.
grab this object Will move the object to the position of the mouse and you can drag it to where you want to place it.

reveal this object Will display the halo for this object so you can find it. This can take a second or two.

tile representing this object
Will create a tile representing the script's Object. Tiles representing an object can be used in this or other scripts to replace the Object being acted upon in a script.

open viewer Will open the viewer for this script's object

destroy this script
Will destroy this script.

4.9 Viewer Category Scripting

Object-start_script.png 
 
This starts the given script ticking. You can select from available scripts at the right of the tile.
Object-pause_script.png 
 
This makes the given script be "paused". You can select from available scripts at the right of the tile. Note: If the script specified is the script containing this tile, the script will not pause until it finishes its current processing of the all the tiles in the script. For example, if you have a "Object forward 5" tile after a "Object pause script script1" tile in a script named "script1", the object will move forward 5.
Object-stop_script.png 
 
This makes the given script stop or revert to "normal". You can select from available scripts at the right of the tile.
Note: If the script specified is the script containing this tile, the script will not stop until it finishes its current processing of the all the tiles in the script. For example, if you have a "Object forward 5" tile after a "Object stop script script1" tile in a script named "script1", the object will move forward 5.
Object-start_all.png 
 
This starts the given script and all of its siblings's scripts ticking in the object. You can select from available scripts at the right of the tile.
Object-pause_all.png 
 
This makes the given script and all of its sibling's scripts be "paused" in the object. You can select from available scripts at the right of the tile.
Note: If the script specified is the script containing this tile, the script will not pause until it finishes its current processing of the all the tiles in the script. For example, if you have a "Object forward 5" tile after a "Object pause all script1" tile in a script named "script1", the object will move forward 5.
Object-stop_all.png 
 
This make the given script and all of its sibling's scripts stop or revert to "normal" in the object. You can select from available scripts at the right of the tile.
Note: If the script specified is the script containing this tile, the script will not pause until it finishes its current processing of the all the tiles in the script. For example, if you have a "Object forward 5" tile after a "Object stop all script1" tile in a script named "script1", the object will move forward 5.  Object-tell_all_siblings.png 
 
This sends a message to all siblings to run the given script once. The object that executes this will not have its script run, only its siblings will run the script once.  You can select from available scripts at the right of the tile.
Object-do.png 
 
This runs the given script once, on the next tick. You can select from available scripts at the right of the tile.
Object-send_to_all.png 
 
This runs the given script in the object and in all of its sibling once. You can select from available scripts at the right of the tile.

No comments: