Basic NetLogo Commands
Procedure Keywords
to <name>

end / Statement used to name and start a procedure
Example: to setup
Statement used to end a procedure.
Clear Commands
clear-all / Deletes all turtles and resets all patches to default values
clear-turtles / Removes all the turtles without erasing the Netlogo surface or changing the patches
clear-patches / Clears the patches by resetting all patch variables to their default values, including setting their color to black.
Create Turtles Commands
create-turtles # / Creates the specified number of turtles at origin and gives a random heading to each turtle
Example: create-turtles 1
sprout # / Patches create turtles. The turtle gets created on the patch that created it.
Example: sprout 2
Turtle Movement Commands
forward # / Tells an agent to move forward a number of patches
Example: forward 1
back # / Tells an agent to move back a number of patches
Example: back 1
pen-down / Tells an agent to put its pen down draw a trail
pen-up / Tells an agent to pick up its pen and stop drawing a trail
setxy # # / Move the turtle to the x-coordinate and y-coordinate given
Example: setxy 10 20
set heading # / Sets heading or direction that the agent faces in degrees relative to the Netlogo coordinate system
Example: set heading 90
right # / Asks the turtle to turn right a certain number of degrees relative to the direction the turtle is facing
Example: right 90
left # / Asks the turtle to turn left a certain number of degrees relative to the direction the turtle is facing
Example: left 90
Turtle Attribute Commands
set color color_name / Specifies or changes the color of the turtles to the specified color (color_name). Some possible color names include red, blue, green, orange, yellow, pink, magenta, lime, gray, and violet.
Example: set color red
set size # / Specifies or changes the size of the turtles to a specified number (#). The default is 1.
Example: set size 3
set pen-size # / Specifies or changes the size of the pen the turtles carry to a specified number (#). The default is 1.
Example: set pen-size 3
set color # / Changes an agent’s color to # (see Netlogo Programming guide for color values). There are 140 colors in Netlogo.
Example: set color to 15 sets the color to bright red
set color R# G# B# / Changes an agent’s color to R# G# B# (RGB notation) where R# stands for how much red is in the color, G# stands for how much green is in the color and B# stands for how much blue is in the color. The R# G# and B# can range between 0 and 255.
Example: set color 255 0 0 sets the color to bright red
Ask Turtle Commands
ask turtles […..] / Asks the turtles to perform the instructions (commands) given in square brackets. Each turtle performs the instructions one at a time.
Example: ask turtles
[forward 1]
ask turtle # [ ] / Asks a specific turtle to perform the commands in brackets.
Patch Attribute Commands
set pcolor # (or name or RGB) / The command line sets the patch color Color can be represented either as a NetLogo color (a single number or name) or an RGB color (a list of 3 numbers).
Example: set pcolor 15 ; set the color of the patch to red
Ask Patches Commands
ask patch # #
[commands] / Asks the specific patch to run the commands given in the command block
Example: ask patch 1 3
[set pcolor 15 ] ; asks patch 1 3 to set its color to red
ask patches
[commands] / Asks the all patches to run the commands given in the command block
Example: ask patchs
[set pcolor 15 ] ; asks all the patches to set their color to red
Patches Reporters
max-pxcor, max-pycor / These reporters give the maximum x-coordinate and maximum y-coordinate, (respectively) for patches, which determines the size of the world.
Example: create-turtles 100
[ setxy random-float max-pxcor
random-float max-pycor ]
;; distributes 100 turtles randomly in the
;; first quadrant
min-pxcor, min-pxcor / These reporters give the minimum x-coordinate and minimum y-coordinate, (respectively) for patches, which determines the size of the world.
Example: creare-turtles 100
[ setxy random-float min-pxcor
random-float min-pycor ]
;; distributes 100 turtles randomly in the
;; third quadrant
patch-ahead # / Give you patch that is the given distance, #, "ahead" of this turtle, that is, along the turtle's current heading. Reports nobody if the patch does not exist because it is outside the world.
Example:
ask patch-ahead 1 [ set pcolor green ] ;; turns the patch 1 in front of this turtle green; note that this might be the same patch the turtle is standing on
Variables Keywords
globals [variable_names] / A keyword that can only be used at the beginning of a program, before any functions or procedures. It defines new global variables. Global variables are "global" because they are accessible by all agents and can be used anywhere in a model. Most often, globals is used to define variables or constants that need to be used in many parts of the program.
Example: globals [NumTurtles ColorTurtles] ;; declares two global variables NumTurtles and ColorTurtles.
turtles-own [variable_names] / A keyword that can only be used at the beginning of a program, before any functions or procedures. It defines new turtle variables unique to each turtle
Example: turtles-own [eyes legs] ;; declares two turtle variables eyes and legs.
patches-own [variable_names] / A keyword that can only be used at the beginning of a program, before any functions or procedures. It defines new patch variables unique to each patch
Example: patches-own [patchColor] ;; declares one patch variable patchColor.
links-own [variable_names] / A keyword that can only be used at the beginning of a program, before any functions or procedures. It defines new link variables unique to each link.
Example: links-own [traffic] ;; declares one link variable traffic.
Get and Set Variables Commands
let local_variable_name value / Creates a new local variable called local_variable_name and gives it the given value. A local variable is one that exists only within the enclosing block of commands such as a procedure or within the ask turtles brackets.
Example: let num1 10
This creates a local variable num1 and gives it an initial value of 10
set variable_name value / Sets variable (variable_name) to the given value.
Example : set num1 25
Changes the value of the local variable num1 to 25
Looping Commands
repeat # [commands] / Repeats the set of commands in the square bracket a certain number (#) of times.
Example: repeat 20
[
right 5
forward 1
]
the turtle turn right 5o and steps forward 1 step 20 times
while [condition] [commands] / Repeats the set of commands in the second set of square brackets while the condition in the first set of square brackets is true. When the condition within the first set of square brackets is false, the loop is exited.
Conditionals Commands
if condition
[commands] / If condition reports true, then the program runs the commands in the command block. The reporter may report a different value for different agents, so some agents may run commands and others don't.
Example: if xcor > 0[ set color blue ]
;; turtles in the right half of the world turn blue
ifelse condition
[commands1]
[commands2] / If condition is true, runs commands1.
If condition is false, runs commands2.
The reporter may report a different value for different agents, so some agents may run commands1 while others run commands2.
Example:
ask patches
[
ifelse pxcor > 0
[ set pcolor blue ]
[ set pcolor red ]
]
;; the left half of the world turns red and the right half turns blue
Random Number Commands
random # / If number is positive, reports a random integer greater than or equal to 0, but strictly less than the number #.
If number is negative, reports a random integer less than or equal to 0, but strictly greater than number.
If number is zero, the result is always 0 as well.
Examples:
show random 3
;; prints 0, 1, or 2
show random -3
;; prints 0, -1, or -2
random-pxcor, random-pycor / Provides a random integer ranging from min-pxcor (or -y) to max-pxcor (or -y) inclusive.
Example: setxy random-pxcor random-pycor
Tick Commands
reset-ticks / Resets the tick counter to zero. Normally reset-ticks goes at the end of a setup procedure
tick / Advances the tick counter by one. If the tick counter has not been started yet with reset-ticks, an error results. Normally tick goes at the end of a go procedure.
ticks / Reports the current value of the tick counter. The result is always a positive number and never negative. If the tick counter has not been started yet with reset-ticks, an error results.

6e. NetLogo Commands for SC KO 2013 (handout).docx