banner-nll

Turtle Variables

NetLogo has several pre--defined turtle varialbes. These include:

breed

This is a predefined turtle variable. It holds the agentset of all turtles of  the same breed as this turtle. (For turtles that do not have any particular breed, this is the turtles agentset of all turtles.) You can set this variable to change a turtle's breed.

See also breeds.

Example:

    breeds [cats dogs]
    ;; turtle code:
    if breed = cats [ show "meow!" ]
    set breed dogs
    show "woof!"

breeds

breeds [breed1breed2 ...]

This keyword, like the globals, turtles-own, and patches-own keywords, can only be used at the beginning of a program, before any function definitions. It  defines breeds and their associated agentsets.

Any turtle of the given breed:

  • is part of the agentset named by the breed name
  • has its breed built-in variable set to that agentset

Most often, the agentset is used in conjunction with ask to give commands to only the turtles of a particular breed.

    breeds [ mice frogs ]
    to setup
      ca
      create-mice 50
      ask mice [ set color white ]
      create-frogs 50
      ask frogs [ set color green ]
      show breed-of one-of mice    ;; prints mice
      show breed-of one-of frogs   ;; prints frogs
    end

See also globals, patches-own, turtles-own, <BREED>-own, create-<BREED>, create-custom-<BREED>, <BREED>-at, <BREED>-here.

color

This is a predefined turtle variable. It holds the color of the turtle. You  can set this variable to make the turtle change color.

See also pcolor.

heading

This is a predefined turtle variable. It indicates the direction the turtle  is facing. This is a number greater than or equal to 0 and less than 360. 0 is  north, 90 is east, and so on. You can set this variable to make a turtle turn.

See also right, left, dx, dy.

Example:

    set heading 45      ;; turtle is now facing northeast
    set heading heading + 10 ;; same effect as "rt 10"

hidden?

This is a predefined turtle variable. It holds a boolean (true or false) value indicating whether the turtle is currently hidden (i.e., invisible). You  can set this variable to make a turtle disappear or reappear.

See also hideturtle, showturtle.

Example:

    set hidden? not hidden?
    ;; if turtle was showing, it hides, and if it was hiding,
    ;; it reappears

label

This is a predefined turtle variable. It may hold a value of any type. The turtle appears in the graphics window with the given value "attached" to it as  text. You can set this variable to add, change, or remove a turtle's label.

See also no-label, label-color, plabel, plabel-color.

Example:

    ask turtles [ set label who ]
    ;; all the turtles now are labeled with their
    ;; id numbers
    ask turtles [ set label no-label ]
    ;; all turtles now are not labeled

label-color

This is a predefined turtle variable. It holds a number greater than or equal  to 0 and less than 140. This number determines what color the turtle's label  appears in (if it has a label). You can set this variable to change the color of a turtle's label.

See also no-label, label, plabel, plabel-color.

Example:

    ask turtles [ set label-color red ]
    ;; all the turtles now have red labels

pen-down?

This is a predefined turtle variable. It holds a boolean (true or false) value indicating whether the turtle's pen is currently down. You can set this variable to put a turtle's pen down or pick it back up again.

See also pen-down, pen-up.

shape

This is a predefined turtle variable. It holds a string that is the name of  the turtle's current shape. You can set this variable to change a turtle's  shape. New turtles have the shape "default" unless the a different shape has been specified using set-default-shape.

See also set-default-shape.

size

This is a predefined turtle variable. It holds a number that is the turtle's apparent size in the graphics window. The default size for a turtle is 1.0,  which means that the turtle is the same size as a patch. You can set this  variable to change a turtle's size.

All turtles appear the same size in the graphics window unless the "Turtle Sizes" checkbox in the graphics window edit dialog is checked. If that checkbox is not checked, you can still use this variable, but it will not have any  visible effect.

Note: the "Turtle Sizes" feature is currently considered experimental. It may  cause your model to run much more slowly, and it may cause display anomalies.

who

This is a predefined turtle variable. It holds the turtles id number (an integer greater than or equal to zero). You cannot set this variable; a turtle's id number never changes.

If a turtle dies, a new turtle may eventually be assigned the same id number that was used by the dead turtle.

You can use the turtle reporter to retrieve a turtle with a given id number. A turtle can refer to itself by saying "turtle who".

Examples:

    show values-from (turtles with [color = red]) [who]
    ;; prints a list of the id numbers of all red turtles
    ;; in the Command Center
    ask random-one-of turtles with [color = red and who != who-of myself]
      [ die ] ;; this turtle kills a random red turtle, but never
             ;; kills itself

See also turtle.

xcor

This is a predefined turtle variable. It holds the current x coordinate of the turtle. This is a floating point number, not an integer. You can set this variable to change the turtle's location.

This variable is always greater than or equal to 0 - screen-edge-x and strictly less than screen-edge-x.

See also setxy, ycor, pxcor, pycor.

ycor

This is a predefined turtle variable. It holds the current y coordinate of the turtle. This is a floating point number, not an integer. You can set this variable to change the turtle's location.

This variable is always greater than or equal to 0 - screen-edge-y and strictly less than screen-edge-y.

See also setxy, xcor, pxcor, pycor

Continue this guide for info about Patch Variables
 


With Support from:

The NetLogo Learning Lab is part of modelingcomplexity.org, the home of the Mesa State College Center for Agent-Based Modeling.

This website is copyright by Mesa State College, 2004. All rights are reserved.

Some materials are adapted from the NetLogo User manual, and are copyright Wilensky, U. (1999). NetLogo.  Center for Connected Learning and Computer-Based Modeling. Northwestern University, Evanston, IL.