GEOG3150 - GIS, Geocomputation and Geoplanning - Semster 2


Table of Contents

1. Creating the world
2. Buttons and Procedures
3. Sliders and Variables
4. Creating Turtles and Patches
5. Making the Model Go

Practical 2, part 2 - Buttons and Procedures

Creating a Button

Adding a button

In this section, you will create a button that will be used to set up the model.

  1. Click on the drop-down list next to 'Add' and select 'Button' (see left image)
  2. Move the mouse to where you would like the button to be and left-click to place it.
  3. In the new menu that appears you can specify what the button should do.

  4. Enter 'setup' in the commands box (without the apostrophes) - this will mean that a procedure called 'setup' is called when the button is clicked. You'll see what this means shortly.
  5. Adding a button
  6. The 'Display Name' is a label for the button. This will be displayed on the button and you can choose any name you like. I have chosen "Setup the Model".
  7. Click on OK to finish the button.
  8. You can see that the button is red which means it refers to a procedure (method, i.e. we will tell it to do something) that hasn't been created yet. We'll do that now.

    Procedures

    One thing that hasn't been covered yet is procedures. These are a way of grouping several commands together that perform a common task. One example is setting up the model: there will probably be some commands to create the turtles, some for the patches, and some to create other parts of the model. We can group these all together in a single procedure.

  9. Click on the 'Code' tab. This is where all the commands for our model go
  10. We are going to create a new procedure called 'setup'.

  11. Enter the following text into the 'Code' tab: to setup
     print "Setting Up Model"
    end
  12. Setup message

    The code above does three things:

    1. The first line (to setup) says whatever comes next will be part of a procedure called 'setup'.
    2. You should be familiar with the second line (print "Setting Up Model") - it will print a message to the command centre.
    3. The third line (end) says that we have come to the end of the 'setup' procedure. Any more commands that come afterwards are not part of 'setup'.
  13. Check this works by clicking on the 'Interface' tab. Has the button text gone from red to black?
  14. Click on the button. You should see a message displayed on the command centre (see right image).

Eh??

It's worth having another look at what is going on above, as this goes to the heart of how NetLogo works. The first thing that we did is create a button. When creating it, we told the button that if someone presses it, it should try to run a procedure called 'setup'.

Then, in the Code tab, we created a new procedure called 'setup'. So, if someone presses the button, all the commands that are part of the 'setup' procedure will run. The image below illustrates this graphically.

Pressing the setup button starts a procedure

Have a go at the activities below. When you're comfortable that you can answer them, move on to part 3.

Activity

  1. Write the NetLogo code that creates a new procedure called 'hello' to print the text "Hello World" to the Command Centre.  
     
     

[School of Geography homepage] [Leeds University homepage]