Guide to Programming in StarLogoT

This page outlines the basic concepts in StarLogoT programming and is a useful resource for syntax questions.


1. Declaring Variables
2. Setting Variables
3. Spaces and Syntax
4. Parentheses and Brackets
5. Commas
6. Comments
7. Constants
8. Arrays
9. Strings
10. Inputs to Procedures

1. Declaring Variables

At the top of the procedures window, each agent (Turtles, Patches, and the Observer) declares its variables for the model. Each agent has some default variables; for example, every Turtle agent has the variables "color" and "heading". However, many models require giving agents new variables. For example, each turtle might have an energy value that is incremented each time it eats and decremented each time it moves. At the top of such a model we would declare this variable with the following text:

Turtles-own [energy] 

You can give each turtle more variables by adding to the list:

Turles-own [energy gender] 

Now each turtle has a gender variable, which might be useful in a reproduction model.
For patches the syntax is the same:

Patches-own [grass-length] 

This variable could be used if each turtle eats some grass from the patch that it is on. The grass-length variable for the patch could then be decremented using the tset-grass-length command. Each turtle would then decrement the grass-length variable of the patch that it is on. See the Setting Variables section below.

If you want to have a global parameter of the model, there are two ways of setting it up:

1) You can declare an Observer variable called a Global. For example, imagine that you want to have a counter that keeps track of each step of the model. If the turtles or patches keep count, then each turtle or patch has its own counter variable, and it is possible that the different counts can become asynchronous. However, if you make a Global variable called 'count', then you can increment the Global count variable once each turn, regardless of the behavior of the turtle and patch agents. Globals are declared with the following syntax:

Globals [count] 

2) You can also create global variables with slider and switch controls in the interface window. When you create a slider or a switch, the corresponding global variable is automatically created, and it can be used anywhere in the model. Refer to Getting Started for instructions on creating slider and switch controls.

Declaring arrays (see below for more on arrays) requires setting the length of the array. This determines how many elements, or variables, an array can hold. Arrays are declared just like other agent variables. So, if in addition to the variables "engery" and "gender" you want each turtle to have an array named "colors" with four elements, the array declaration has the following syntax:

turtles-own [energy colors[4] gender]

2. Setting Variables

An agent can change a variable that it owns by using the "set" syntax. For example, all turtles have a color variable, so the following code tells each turtle that has an energy level of at least 12 to set its color to red.

if (energy >= 12) [setcolor red]
However, sometimes you will want an agent to access and change the variable of another agent. The following code snippet tells each turtle to set the energy of any turtle directly above it to 12.
setengery-at 0 1 12

An agent can also set the values for variables that are owned by other types of agents. In a model where turtles eat grass, each turtle agent will need to decrement the value of the grass-length for the patch it is on. The syntax for this is the following:

tsetgrass-length (grass-length - 1)
"tset" is used when a turtle agent changes a patch variable. The above snippet tells each turtle to set the grass-length on the patch it currently occupies to it's current length minus 1. If multiple turtles are on a patch, the length will be decremented mutiple times, which is precisely the behavior we want.

Just like "tset", "pset" is used by patches setting turtle variables, and "oset" is used when the observer sets turtle or patch variables.

The maximum numerical value a variable can hold is 32767. The minimum is -327678.

Arrays, discussed below, have their own syntax for setting variables. Since arrays are lists of variables, you must pick out the specific item in the array that you wish to set. So, for example, if each turtle has an array of four elements called "colors", and you wish to set the first element of the array to the color of the turtle, the syntax is as follows:

setcolors-ref 0 color
The numbering of the array elements begins with "0" for the first spot, and finishes with "length - 1" for the last spot. As with other variables, agents can set the values for elements of another agent's array using the setcolors-ref-at and the setcolors-ref-of syntax (for more on '-at' and '-of' syntax see the Guide to the Primitives). To put the color of the agent whose ID-number is 45 in the second spot of each turtle's array, the syntax is:
setcolors-ref-of 45 color
To have each turtle put its color into the third element of the array belonging to the turtle that is directly above it, the syntax is:
setcolors-ref-at 3 0 1 color
All of the agents can use the "tset", "pset", and "oset" syntax just as above to set other agents' array elements.

3. Spacing and Syntax

There are no reserved symbols in StarLogoT, therefore"+/-=" is a valid variable name. Given this freedom, there is a required coding convention regarding the use of spaces. All variables, operators, and primitives must have spaces between them (i.e, (16 / 12) + 3) . In all the StarLogoT code examples, you will notice this convention followed rigorously. The only exceptions are brackets and parentheses. Similarly, names for procedures and variables must NOT have spaces. Hence the liberal use of dashes to create legible compound words with no spaces, such as patches-own, grass-length, etc.

4. Parentheses & Brackets

Parentheses can be used liberally; they insure precedence and can be used as scope resolution operators. They are commonly used to demarcate the condition to be satisfied by an "if" statement (e.g., if (color = red) [die]). It is therefore a good idea, when encountering a mysterious bug, to consider that a failure to use parenthases resulted in an unexpected precedence of operations. For example:

 random 10 + 5 
might move all turtles random 10, and then 5, i.e, (random 10) + 5.
Or, it might move them random 15, i.e., random (10 + 5).
Parenthases resolve this ambiguity.

Brackets are required to encase all lists, as in turtles-own and patches-own, and all lists of instructions, as in the following:

 if (color = red) [rt 10 fd 5 setcolor blue]
They are most often used to open and close the executed content of a function or an "if" statement. A common bug in StarLogoT programming is to leave out brackets when you need them.

5. Commas

The comma is an often-misunderstood StarLogoT operator. It is used to synchronize the parallel operations of turtles and patches. In effect, it enforces a "wait point" in the StarLogoT parallel execution. Turtles and patches will "wait" at a comma until all other turtles and patches have reached that point before continuing execution. For an example of why the comma operator can be important, look in at the "Patch Communications Demo" sample model in the included code examples.

6. Comments

Semicolons are used to comment out text in the procedures window. They are used in the code examples throughout this manual. Any text appearing after a semicolon until the end of the line is treated as a comment.

For Example:

 Repeat 36 [rt 10 fd 4]  ;turtles make a polygon 
You can put in more than one semicolon to make the comment stand out. Generally, the more comments the better.

7. Constants

Constants are like Global variables, but they do not change. Every agent has access to a constant. Constants are declared, like the variables, at the top of a model's procedures. In a model where it would be useful, for example, to have the value of gravity used by different agents in calculating their behavior, we can define the constant "gravity" with the following syntax:

constants [gravity [9.81]] 

Now, the term "gravity" can be used throughout the model to substitute for the value "9.81"

8. Arrays

Arrays are ordered lists of variables whose contents are individually indexed. Like regular lists, arrays can be owned by any agent. To create an array, include the array name, followed by the maximum index of the array (in brackets) within the variable list for a particular agent. For example:

globals [
  global-array [50]
]

turtles-own [
  turtle-array [100]
]
To retrieve the value of a particular element in an array, use the following syntax:
arrayname-ref(-at, -of) index
To set the value of a particular element in an array, use this syntax:
setarrayname-ref(-at, -of) index value
The index is the spot in the array that is being accessed. Indexing starts at 0, so the value of index ranges from 0 to (array length - 1). The value is the value to which an array item will be set. For example:
; to set the first item in global-array to 100
setglobal-array-ref 0 100

; to set the last item in global-array to 100
setglobal-array-ref 50 100

; this command is illegal
setglobal-array-ref 51 100

9. Strings

A string is a set of characters that makes up a word or a sentence. For example, "M", "Miss", and "Miss Muffet" are all strings. To create a one-word string in StarlogoT, use the quote (") character. To create a multiple-word string, or a string with special characters in it (such as the quote character itself), use the quote character followed by a vertical bar (|), and end the string once again with a vertical bar. For example:

globals [my-string]
setmy-string "M
setmy-string "Miss
setmy-string "|Miss Muffet|
Notice that StarlogoT does not require a concluding quote character. In fact, a concluding quote character will generate an error. Notice also that the quote character alone will not preserve capitalization. For best results, use "|string| notation all the time.

10. Inputs to Procedures

Procedures in StarLogoT can take inputs. An input is specified by creating a temporary variable that begins with a colon. For example:

to walk  :speed  :direction 
  setheading :direction 
  fd :speed 
end 

When walk is called from another procedure, it needs to be called with two arguments that will satisfy the input requirements "speed" and "direction".

to go
  walk 5 (random 360)
end