NetLogo Variables and Properties

New Properties for Turtles and Patches:

You can give Turtles and Patches new properties, in addition to the ones they're born with.  For instance, Turtles are born with lots of properties, like xcor and color, but suppose you want to divide your Turtles into two groups by gender, for your particular simulation model.  You can do that by giving all Turtles a new property called gender, and then later setting the gender property to any value for each turtle.  In the example below, we'll create 10 Turtles and give them a "male" value for their gender property, and 20 Turtles with "female" as their gender value.  Later, we can make the males and females behave differently: the males will walk around more drunkenly than the females...

turtles-own [gender]

to Setup
  crt 10 [set gender "male"]
  crt 20 [set gender "female"]
end

to Go
  if gender = "male" [
    fd 1
    rt random 50
    lt random 50]
  if gender = "female" [
    fd 1
    rt random 10
    lt random 10]
end

The command turtles-own [gender] should appear at the very top of your code (as the 1st or 2nd or 3rd line of code).  You can create several new properties at once by including several property words in the same command, separated by spaces, as in: turtles-own [gender  hair-length  Hogwarts-house].  Properties can have numeric values (in fact, all new properties are initially set to 0, and will be reset to 0 by the command CA) , string values (like "male"), truth values (like True and False), and many other types of data values (e.g. agent-sets, lists, and others).

Of course, the same thing holds for new properties for patches:  patches-own [fcolor  altitude], etc.

Global Variables:

Global variables come in 2 types: a) those that are created in the Interface tab when creating controls, like sliders and choosers, and b) those that are created in the Code tab, in the first few lines, near the turtles-own and patches-own commands.  The discussion below concerns only the Code tab global variables.

Suppose you've created a game and want to keep track of the user's score and health-points.  As the game progresses, the user's score and health-points may rise and fall, depending upon what she does while playing the game.  So, we'll create two global variables, and use them to keep track of the user's score and health:

globals [score  health-points]

to Setup
  set score 0
  set health-points 100
end

to Go
   ; Here, we write code to control the user's turtles, etc.
   ; Any time turtle 0 lands on a red patch, the user loses health, and any time turtle 1 lands on a blue
   ; patch, her score gains 10 points:
  ask turtle 0 [
    if pcolor = red [set health-point health-points - 1]]
  ask turtle 1 [
    if pcolor = blue [set score score + 10]]
   ; if her health declines to 0, the game is over
  if health-points = 0 [
    ask turtles [die]
    user-message "Game Over!"
    stop]
end

These 2 global variables, score and health-points are available to all procedures.  They're given a default value of 0 when the program starts, but can be changed in any procedure to any value, and will retain that value until set to another value sometime later.  Use the set command, as in the examples above to change their values.

 

Local Variables:

Local variables are created inside procedures, to be used by those procedures.  For instance, suppose you want to create a procedure called Print5 to print the five numbers 1 through 5 in the Command Center (bottom of the Interface tab):

to Print5
  let fred 1
  Repeat 5 [
    print fred
    set fred fred + 1
  ]
end

To create the fred variable, we use the let command, that creates the variable and immediately sets it to a value.  Thereafter, once fred has been created, we can use the set command to change its value.

When the Print5 procedure exits (finishes), the fred variable is eliminated (fred dies), and a new version of fred will be created the next time that Print5 is called.