Common Netlogo Commands:


setxy x y
Turtle Command

The turtle sets its x-coordinate to x and its y-coordinate to y.

Equivalent to set xcor x set ycor y, except it happens in one time step instead of two.

If x or y is outside the world, NetLogo will throw a runtime error.

setxy 0 0
;; turtle moves to the middle of the center patch
setxy random-xcor random-ycor
;; turtle moves to a random point
setxy random-pxcor random-pycor
;; turtle moves to the center of a random patch

ask agentset [commands]
ask agent [commands]

The specified agent or agentset runs the given commands.

ask turtles [ fd 1 ]
;; all turtles move forward one step

ask patches [ set pcolor red ]
;; all patches turn red

ask turtle 4 [ rt 90 ]
;; only the turtle with id 4 turns right

Note: only the observer can ask all turtles or all patches. This prevents you from inadvertently having all turtles ask all turtles or all patches ask all patches, which is a common mistake to make if you're not careful about which agents will run the code you are writing.

Note: Only the agents that are in the agentset at the time the ask begins run the commands.

clear-turtles
Observer Command

Kills all turtles.

Also resets the who numbering, so the next turtle created will be turtle 0.

die
Turtle Command Link Command

The turtle or link dies.

if xcor > 20 [ die ]
;; all turtles with xcor greater than 20 die
ask links with [color = blue] [ die ]

create-turtles
crt
create-<breeds>

create-turtles number
create-turtles number [ commands ]
create-<breeds> number
create-<breeds> number [ commands ]
Observer Command

Creates number new turtles at the origin. New turtles have random integer headings and the color is randomly selected from the 14 primary colors.

If the create-<breeds> form is used, the new turtles are created as members of the given breed.

If commands are supplied, the new turtles immediately run them. This is useful for giving the new turtles a different color, heading, or whatever. (The new turtles are created all at once then run one at a time, in random order.)

crt 100 [ fd 10 ] ;; makes a randomly spaced circle
breed [canaries canary]
 breed [snakes snake]
to setup
clear-all
create-canaries 50 [ set color yellow ]
create-snakes 50 [ set color green ]
end

;; all the blue links will die

show

show value

Prints value in the Command Center, preceded by the calling agent, and followed by a carriage return. (The calling agent is included to help you keep track of what agents are producing which lines of output.) Also, all strings have their quotes included similar to write.



set-default-shape

set-default-shape turtles string
set-default-shape breed string
Observer Command

Specifies a default initial shape for all turtles, or for a particular breed. When a turtle is created, or it changes breeds, it shape is set to the given shape.

This command doesn't affect existing turtles, only turtles you create afterwards.

The specified breed must be either turtles or a breed defined by the breed keyword, and the specified string must be the name of a currently defined shape.

In new models, the default shape for all turtles is "default".

Note that specifying a default shape does not prevent you from changing an individual turtle's shape later; turtles don't have to be stuck with their breed's default shape.

create-turtles 1
;; new turtle's shape is "default"
create-cats 1
;; new turtle's shape is "default"
set-default-shape turtles "circle"
create-turtles 1
;; new turtle's shape is "circle" create-cats 1
;; new turtle's shape is "circle"
set-default-shape cats "cat"
set-default-shape dogs "dog" create-cats 1
;; new turtle's shape is "cat"
ask cats [ set breed dogs ]
;; all cats become dogs, and automatically
;; change their shape to "dog"

tick-advance

tick-advance number
Observer Command

Advances the tick counter by number. The input may be an integer or a floating point number. (Some models divide ticks more finely than by ones.) The input may not be negative.

See also tick, ticks, reset-ticks.


n-of

n-of size agentset
n-of size list

From an agentset, reports an agentset of size size randomly chosen from the input set, with no repeats.

From a list, reports a list of size size randomly chosen from the input set, with no repeats. The items in the result appear in the same order that they appeared in the input list. (If you want them in random order, use shuffle on the result.)

It is an error for size to be greater than the size of the input.

ask n-of 50 patches [ set pcolor green ]
;; 50 randomly chosen patches turn green

reset-ticks

reset-ticks
Observer Command

Resets the tick counter to zero.

See also tick, ticks, tick-advance.

if

if condition [ commands ]

Reporter must report a boolean (true or false) value.

If condition reports true, runs commands.

The reporter may report a different value for different agents, so some agents may run commands and others don't.

if xcor > 0[ set color blue ]
;; turtles in the right half of the world
;; turn blue

ifelse

ifelse reporter [ commands1 ] [ commands2 ]

Reporter must report a boolean (true or false) value.

If reporter reports true, runs commands1.

If reporter reports false, runs commands2.

The reporter may report a different value for different agents, so some agents may run commands1 while others run commands2.

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

See also if, ifelse-value.

patch-here

patch-here
Turtle Command

patch-here reports the patch under the turtle.

Note that this reporter isn't available to a patch because a patch can just say "self".

face

face agent
Turtle Command

Set the caller's heading towards agent.

If wrapping is allowed by the topology and the wrapped distance (around the edges of the world) is shorter, face will use the wrapped path.

If the caller and the agent are at the exact same position, the caller's heading won't change.

facexy

facexy number number
Turtle Command

Set the caller's heading towards the point (x,y).

If wrapping is allowed by the topology and the wrapped distance (around the edges of the world) is shorter and wrapping is allowed, facexy will use the wrapped path.

If the caller is on the point (x,y), the caller's heading won't change.

random

random number

If number is positive, reports a random integer greater than or equal to 0, but strictly less than 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.

Note: In versions of NetLogo prior to version 2.0, this primitive reported a floating point number if given a non-integer input. This is no longer the case. If you want a floating point answer, you must now use random-float instead.

show random 3
;; prints 0, 1, or 2
show random -3
;; prints 0, -1, or -2
show random 3.5
;; prints 0, 1, 2, or 3