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 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]
List |
globals [variable1 variable2 variable3]
Turtle, Observer, Patch |
let [:variable value]
String | the variable name that you will use | |
Number | the value that you are setting the variable to |
variable
value
let [:variable1 value1 :variable2 value2]
let [:myvar 6]
:myvar
Observer |
patches-own [variable-list]
Variable | a list of patch variables separated by spaces |
patches-own [size value]
patches-own
patches-own
Turtle, Observer, Patch |
set set value
Variable | variable that gets redefined | |
Anything | value to be assigned to variable |
set energy 5
energy
5
energy
Turtle, Observer, Patch |
setslidervar value
Number | The value you want to set the slider to |
setnumber-of-turtles 100
number-of-turtles
100
Turtle, Observer, Patch |
setvariable value
Anything | the value to set the variable to |
variable
setheight 5
Turtle, Observer, Patch |
setvariable-at xcor ycor value
Number | number of steps in the x-direction from the caller | |
Number | number of steps in the y-direction from the caller | |
Anything | value to set the variable to |
variable
setheight-at 1 1 5
Turtle, Observer, Patch |
setvariable-of number value
Number | who of a turtle | |
Anything | value to set the variable to |
variable
setspots-of 5 10
Turtle, Observer, Patch |
setvariable-towards angle distance value
Number | angle from caller | |
Number | distance from caller | |
Anything | value to set the variable to |
variable
sethappiness-towards 0 1 5
Turtle, Observer |
to procedure-name
Anything | A single word which is the name of the procedure you wish to define |
to procedure-name
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
:z
All procedures must be ended with the word end
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.
to go
fd 1
rt 90
end
go
Turtle |
turtles-own [list of variables]
List | A list of variable names to define as turtle variables |
A variable name may be followed by a list of state values
turtles-own [size energy]
turtles-own [weather [sunny cloudy rainy]]
set weather rainy
set weather cloudy
if weather = sunny [ setcolor yellow ]
turtles-own
turtles-own
turtles-own
turtles-own
Turtle, Observer, Patch |
variable
variable
height
Turtle, Observer, Patch |
variable-at xcor ycor
Number | number of steps in the x-direction from the caller | |
Number | number of steps in the y-direction from the caller |
variable
size-at 1 1
Turtle, Observer, Patch |
variable-of number1
Number | turtle who number |
variable
height-of 0
Turtle, Observer, Patch |
variable-towards angle distance
Number | angle from caller | |
Number | distance from caller |
variable
happiness-towards 0 1