Command List

State Variables

State variables are built in characteristics of turtles and patches.

Turtles have 7 state variables:

xcor
ycor
color
heading
breed
shown?
pendown?

Patches have 3 state variables:

xcor
ycor
patch-color (pc)

The observer has no state variables.

You can have access to a state variable by using its name in an expression. For example:

xcor + 5
heading - 90
if pendown? [fd 1]

All state variables, with the exception of the xcor and ycor of a patch, can be changed using the set command. For example:

set color red
set pc pc + 1

Note that the set command performs the same function whether it is separated by a space from the variable or not.

Creating New Variables

You can create new state variables for turtles, patches, and the observer. Whenever you create a new variable, StarLogo automatically creates a collection of new procedures to facilitate the use of the variable. For example, if you create the variable age for the turtles, then the turtles can use the procedure setage to change the value of the variable, and they can use the procedure age to access the value of the variable.

You create new variables with the commands turtles-own, patches-own, and globals. You can type these commands in either the turtle procedures window or the observer procedures window (but not both), like this:

turtles-own [age speed]
Creates turtle variables age and speed

patches-own [food]
Creates patch variable food

globals [clock]
Creates global variable clock

For turtle variables, the example age is used.  For patch variables, the example food is used.  And, for global variables, the example time is used.  In order to use the commands listed below, you must first create the variables you want.

Variable Suffixes

You can access or set the state variable of another turtle or patch by using -at, -to , or -towards suffixes. For example:

seth towards xcor-of 23 ycor-of 23

This command will set the heading of the caller toward turtle #23.

set heading-at 2 3 90

This command will set the heading of all the turtles +2 patches away in the x direction and +3 units away in the y direction from the caller to 90 degrees.

set pc-towards 270 4 green

This command will set the patch color of the patch 270 degrees from the current heading and 4 patches away from the caller to green.

-of takes a turtle ID number as its argument.

-at takes a dx and a dy as its arguments, which are relative to the position of the caller. The observer is considered living on patch (0,0).

-towards takes an angle in degrees and a distance in turtle steps. The angle is relative to the heading of the caller.

Local Variables

Like globals, local variables are not associated with any specific patch or turtle. However, the exist only within the procedure within which they are declared.

For example:

to check
let [:num who + color]
if :num > who * 2 [setc red]
end


or

to check
let [:num who + color]
ifelse :num > who * 2
[setc red]
[set :num who * color
if :num > who * 2 [setc red]]
end

Parameters

Parameters are like local variables, in that they only exist during the procedure they are associated with. They are "declared" when the caller of procedure gives them a value. You can pass more than one parameter to a procedure by separating the variable names with a space.

For example:

to move :number
fd :number
lt random 360
bk :number
rt random 360
end


to go
move 30
end

Here are a list of commands that affect variables:

Turtle, Observer

globals [variable-list]

Parameters:
[variable-list] List

Description:
Creates global variables. The variable-list contains one or more names. The globals declaration should be placed at the top of the observer procedures window or the turtle procedures window.

Examples:
globals [variable1 variable2 variable3] Creates globals variable1, variable2, and variable3.

Related Commands:
patches-own turtles-own
Turtle, Observer, Patch

let [:variable value]

Parameters:
[:variable String the variable name that you will use
value] Number the value that you are setting the variable to

Description:
Declares variable as a local variable and assigns value to it. All local variables must have names which begin with a colon. You may declare more than one variable in a let statement (ie. let [:variable1 value1 :variable2 value2]. Variables are declared in the order they appear in the let statement. See the Variables page for more information on local variables.

Examples:
let [:myvar 6] declares a local variable :myvar and assigns its value to 6.

Related Commands:
set
Observer

patches-own [variable-list]

Parameters:
[variable-list] Variable a list of patch variables separated by spaces

Description:
Defines a set of variables to be properties of patches. The [variable-list] contains one or more names separated by spaces

Examples:
patches-own [size value] declares two patch variables - size and value.

Notes:
patches-own must only be used once in any program, and remain outside of any procedures. Typically patches-own is defined once at the top of the Observer Procedures window.

Related Commands:
breeds globals turtles-own
Turtle, Observer, Patch

set set value

Parameters:
set Variable variable that gets redefined
value Anything value to be assigned to variable

Description:
Assigns value to set, where set is any variable which has already been declared. For more information on local variables, see the Variables page.

Examples:
set energy 5 makes the variable energy have value 5 for every turtle, assuming energy has been declared as a turtle variable.

Related Commands:
let setslidervar setvariable setvariable-at setvariable-of setvariable-towards variable
Turtle, Observer, Patch

setslidervar value

Parameters:
value Number The value you want to set the slider to

Description:
Sets the value of a slider variable (named in the command name) to value from within code.

Examples:
setnumber-of-turtles 100 would set the value of the slider number-of-turtles to 100.

Related Commands:
set
Turtle, Observer, Patch

setvariable value

Parameters:
value Anything the value to set the variable to

Description:
Sets the variable to the value specified

Examples:
setheight 5 Sets the height of the caller to 5.

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
set setvariable-at setvariable-of setvariable-towards variable
Turtle, Observer, Patch

setvariable-at xcor ycor value

Parameters:
xcor Number number of steps in the x-direction from the caller
ycor Number number of steps in the y-direction from the caller
value Anything value to set the variable to

Description:
Sets the value of the variable of the turtle xcor units away in the x direction and ycor units away in the y direction to the value value

Examples:
setheight-at 1 1 5 sets the height of the turtle one unit away in the x direction and one unit away in the y direction to 5

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
set setvariable setvariable-of setvariable-towards variable-at
Turtle, Observer, Patch

setvariable-of number value

Parameters:
number Number who of a turtle
value Anything value to set the variable to

Description:
Sets the variable specified of the turtle number to the value value

Examples:
setspots-of 5 10 sets the number of spots of turtle number 5 to 10

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
set setvariable setvariable-at setvariable-towards variable-of
Turtle, Observer, Patch

setvariable-towards angle distance value

Parameters:
angle Number angle from caller
distance Number distance from caller
value Anything value to set the variable to

Description:
Sets the value of the variable specified at distance distance and angle angle from the caller to value

Examples:
sethappiness-towards 0 1 5 sets the happiness of the turtle directly in front of and one space beyond the caller to 5

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
set setvariable setvariable-at setvariable-of variable-towards
Turtle, Observer

to procedure-name

Parameters:
procedure-name Anything A single word which is the name of the procedure you wish to define

Description:
When you create a procedure, the first line of it must be to procedure-name where procedure-name is a one-word title for your procedure. procedure-name cannot be any command already recognized by StarLogo (this includes both built-in commands and the procedure names of other procedures you may already have written).

To add parameters to a procedure, write them after the procedure-name with a colon in front of them, like this:
to my-procedure :x :y :z

Inside the procedure body, you may use :x, :y and :z to refer to the values of the arguments passed in by the caller of the procedure.

All procedures must be ended with the word end on a line by itself (no comments may be on that line).

Procedures defined in the turtle procedures pane are only callable by turtles. Procedures defined in the observer procedures pane are only callable by the observer. Patches may not have procedures.

Examples:
to go
fd 1
rt 90
end

Now you may use go as a command in other parts of your program.

Related Commands:
end output startup stop
Turtle

turtles-own [list of variables]

Parameters:
[list of variables] List A list of variable names to define as turtle variables

Description:
Defines a set of variables to be properties of turtles. The [list of variables] contains one or more names. This line of code should be placed at the top of the Turtle Command Center. It does not go inside procedures.


A variable name may be followed by a list of state values. This variable may only have the values listed in the state value list. This can be used to assign named constants to a variable instead of using numbers.

Examples:
turtles-own [size energy]

turtles-own [weather [sunny cloudy rainy]]

set weather rainy
set weather cloudy
if weather = sunny [ setcolor yellow ]

Notes:
turtles-own declarations should appear outside of any procedure, usually at the top of the turtle procedures pane.

turtles-own declarations can contain more than one variable. If you have more than one turtles-own, consider consolidating them into a single turtles-own.

Related Commands:
breeds globals patches-own
Turtle, Observer, Patch

variable

Description:
Returns the value of the variable specified

Examples:
height returns the value of the variable named height

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
set setvariable variable-at variable-of variable-towards
Turtle, Observer, Patch

variable-at xcor ycor

Parameters:
xcor Number number of steps in the x-direction from the caller
ycor Number number of steps in the y-direction from the caller

Description:
Returns the value of the variable specified at a distance of xcor in the x direction and ycor in the y direction from the caller

Examples:
size-at 1 1 returns the size of the turtle one patch away in the x direction and y patch away in the y direction

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
setvariable-at variable variable-of variable-towards
Turtle, Observer, Patch

variable-of number1

Parameters:
number1 Number turtle who number

Description:
Returns the value of the variable specified for the turtle with who number number1

Examples:
height-of 0 returns the height of turtle number 0

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
setvariable-of variable variable-at variable-towards
Turtle, Observer, Patch

variable-towards angle distance

Parameters:
angle Number angle from caller
distance Number distance from caller

Description:
Returns the value of the variable specified at an angle of angle and distance of distance away from the caller

Examples:
happiness-towards 0 1 returns the happiness of the turtle directly in front of and one space beyond the caller

Notes:
variables must first be declared in a turtles-own, breeds-own, patches-own or globals

Related Commands:
setvariable-towards variable variable-at variable-of