UserPreferences

NetLogoExamples


Computer languages are best described by example. This page gives examples to illustrate concepts described in UsingNetLogo.

1. Defining variables

globals [time randomize-initial-state?]
patches-own [state resource-level]
Remember, agents have built-in variables: xcor, ycor, color, heading for turtles; pxcor, pycor, pcolor for patches.

2. Defining procedures

to update
     command-1
     command-2
     command-3
end

3. Commands

   ask patches [
      task-1
      task-2
   ]

4. Things you can do

   set state (random 5)   ; assigns random integer 0 to 4.
   set time (time + 1)    ; increments variable time
   set pcolor blue        ; turns patch blue
   right random 360       ; changes turtle's heading a random number of degrees
   forward 2              ; moves turtle in direction of heading, two distance units

5. How do you specify whom you're asking?

   ask turtles with [TRUE/FALSE TEST] [TASK]
   ask patches with [TRUE/FALSE TEST] [TASK]
   ask breeds with [TRUE/FALSE TEST] [TASK]
   ask neighbors4 with [resource-level > 3] [set resource resource + (random 2) + 1]

6. Loops

   while [TRUE/FALSE TEST] [
      task-1
      task-2
   ]
So, when do you use loops?
   set i 0
   while [i < 5][
      task-1
      task-2
      set i (i + 1)
   ]

7. Conditionals

   if (TRUE/FALSE TEST) [
      task-1
      task-2
   ]
or
   ifelse (SOME TRUE/FALSE TEST) [
      THINGS TO DO IF TRUE
   ]
   [
      THINGS TO DO IF FALSE
   ]

8. What are all these TRUE/FALSE TESTs?

9. Interface stuff