Variables (Printable)
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 variablesage
andspeed
patches-own [food]
Creates patch variablefood
globals [time]
Creates global variabletime
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.
age [Turtle]
Returns the value of the caller's age
variable.
age-at xcor ycor
[Observer, Turtle]
Returns the value of the variable age
of the turtles at xcor
ycor
relative to the caller.
age-of turtle id number
[Observer, Turtle]
Returns the value of the variable age
of the turtle with who
number turtle id number
.
age-towards angle distance
[Observer, Turtle]
Returns a list of all the turtles and the values of their age
variables which are at the patch distance
away at angle angle
.
food [Observer, Turtle]
Returns the value of the patch variable food
.
NOTE: This is a patch command. It must be called from within an ask-patches
statement.
food-at xcor ycor
[Observer, Turtle]
Returns the value of the variable food
of the patch at xcor
ycor
relative to the caller.
food-towards angle distance
[Observer, Turtle]
Returns the value of thefood
variable of the patch distance
away at angle angle
.
let variable value
[Observer, Turtle]
Declares variable
as a local variable and assigns value
to it. All variables must have names which begin with a colon. See the Variables
page for more information on local variables.
Example:
let [:myvar 6]
set variable value
[Observer, Turtle]
Assigns value
to variable
, where variable
is a local variable which has already been declared. For more information on
local variables, see the Variables page.
setage anything
[Turtle]
Sets the turtle's age
variable to anything
.
setage-at xcor ycor anything
[Observer, Turtle]
Sets the variable age
of turtles at xcor ycor
relative
to the caller to anything
.
setage-of turtle id number anything
[Observer, Turtle]
Sets the variable age
of turtle with who number turtle
id number
to anything
.
setage-towards angle distance anything
[Observer,
Turtle]
Sets the variable age
of turtles distance
away at
angle angle
relative to the caller to anything
.
setfood anything
[Observer, Turtle]
Sets the variable food
of patches to anything
.
NOTE: This is a patch command. It must be called from within an ask-patches
statement
setfood-at xcor ycor anything
[Observer, Turtle]
Sets the variable food
of the patch at xcor ycor
relative to the caller to anything
.
setfood-towards angle distance anything
[Observer,
Turtle]
Sets the variable food
of the patch distance
away
at angle angle
relative to the caller to anything
.
settime anything
[Observer]
Sets the value of the globals
variable time
to anything
.
time [Observer]
Returns the value of the globals
variable time
.
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.
Example:
to check
let [: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.
Example:
to move :number
fd :number
lt random 360
bk :number
rt random 360
end
to go
move 30
end