Tymless Tutorial: Data



   
  < local & global

Private / Public

 
constants >
 

Drawing

Control

Data

Reference

 

When you create a child-turtle and set it off to create a side-drawing, its location, direction etc will naturally change. Meanwhile the parent stays in the same location, facing the same direction etc. These aspects of a turtle can be accessed through system variables such as $here (location) and $! (direction), which are said to be 'private' to each turtle.

In effect, each child-turtle makes a copy of its parent's system variables, such as $here, and any changes are made to the copy rather than the original. When control passes back to the parent turtle the original values remain intact.

User-defined variables are also private by default. Suppose you create a variable Z while controlling a baby-turtle, and you give Z the value "fred". When control passes back to the parent it will not even be aware there is a variable called Z.

Sometimes, though, you will want a parent turtle to be able to access a value set by its progeny. These are called 'public' variables. There is only one copy of a public variable, and this can be read and changed by any turtle. To create a public variable, use the public keyword. This must be done before the variable has been given a value. E.g.

public maxX, maxY
public currentColour=$c
...

 

   
  < local & global
 
constants >