So far everything we've done has been just in the interface tab
and the problem is that when we write all those commands in the command centre
and then we close NetLogo, as you may have found out, when you come back in
it's no longer there and you no longer have those commands.
So what we need to do is start editing in the code tab,
which is where we can then save the results into a model file
so the code that we're writing will be preserved...
for when we want to modify it again in the future.
So let's click on the code tab.
The code tab is where, as you might expect, we actually write the code.
What we do is define procedures.
Procedures are like new commands for the NetLogo model.
There are two procedures that we almost always define right off the bat,
they are the same as the two buttons you see in almost every NetLogo model,
setup and go.
You define a procedure by starting with the word 'to'
and then you write the name of the procedure ('to setup')
and then when you're done with the procedure you write the word 'end'.
So I'm going to go ahead and put stubs in for our setup and go procedures.
And as you can see, NetLogo automatically indents you a couple of spaces,
this is standard NetLogo style to indent a little bit each procedure
or sub-piece of code. That way it makes it easier to see when the format is changing.
One of the first things you always write in the setup procedure...
is the 'clear-all' command, or you can abbreviate it to 'ca' if you want,
and what that does is destroy the model state as we talked about,
and start the model all over again.
And then, once you have the model state changed back to the initial state,
now I can go ahead and start writing my model, because I know the world is blank.
So I can do that, for instance, by creating a group of new turtles,
'crt 100'
and one thing that we could do within that, that we haven't talked about before,
is immediately ask those turtles to do something.
So I can put these square brackets right after the 'create-turtles' command
and that's as if you're putting an implicit 'ask' to the turtles I just created.
So, for instance, I could ask them to set their xy coordinates to random values
'setxy random-xcor random-ycor'
I could also ask them to change their color, for instance, to blue
'set color blue'
and I can have them change their shape. We haven't talked about this yet,
but NetLogo comes with a wide variety of shapes that you can use for your turtles,
the standard triangle you've been seeing is the default shape,
but if you go up to the 'tools' menu, and then go down to 'turtle shapes' editor
you can see a bunch of other shapes that exist within the system.
One that I often use is the 'person' shape, so you can set the shape to 'person'
'set shape "person"'
And now that I've written this code, I can hit the 'check' tab
and what the 'check' tab will do is check to see if there are any syntax errors.
Right now there are not... but let's say I forgot the hyphen in 'random-xcor',
it would tell me that the command, 'RANDOMXCOR', doesn't exist,
and then I might be 'oh yes, there needs to be a hyphen there',
and once I put it back in, it's OK.
So that allows me now to create my basic first command,
and if I go back to the interface tab now I can type 'setup'...
and there you go - I have 100 little people scattered throughout the world.
Now unlike the triangles shapes, the people do not have a particular direction,
so they're always facing up, even though they are actually facing different directions
it's just the objects themselves are what NetLogo calls non-rotatable,
they just stay upright in this particular context.
That could be fine, it's up to you as to what kind of shapes you want,
if you wanted to change that for some reason, you can go to the turtle shapes editor,
go down to "person"
hit 'edit', and make them rotatable.
That will allow them to take on a particular heading.
But let's leave it as it is for now.
So we have a setup procedure, let's write our go procedure,
and we'll just write a simple command, ask turtles forward 5 right random 90 degrees
'ask turtles [ fd 5 rt random 90 ]'
and now, if we go back to the interface tab, we can type 'setup'...
and then 'go', 'go', 'go', and we can see them moving.
If you want to see them moving a little bit, you can run the speed slider down...
and you can see them actually moving around a little bit more.
Now once we've created these two commands, setup and go
we probably want to interact with them more directly through the interface tab,
rather than typing 'setup' and 'go' all the time,
and we can do that by creating two new buttons in the interface tab,
which we talked about before.
When we create a button we have to specify what command it is going to execute.
So in this case we want one button that is going to execute setup,
and we want one button that executes go.
So now if we hit setup and go we keep running the commands.
Now one small note. You can right-click or control-click to pull up 'edit' on a button,
I could give the button a different display name from the command that's executed,
so this could be 'My cool go', and that's what it will say,
even though it's still executing the command 'go'.
If you don't put anything for the display name it will just use the command as the name.
There's also a button here called Forever
What Forever does is say 'keep executing this command until I push the button again'
So if I hit setup now, and then I hit go you'll see it just keeps running,
and then when I hit go again it stops.
At this point you probably want to save your model,
so go up to the 'file' menu and pick 'save',
and then you can give your model a name.
So I'm going to go to my desktop, and call the model 'my-first-model.nlogo'.
And you might also want to start updating the info tab at the same time,
to explain what going on,
so this is 'my first model to explore Netlogo'.
The reason why you want to update the info tab is because you want to make sure...
that you always have documentation that matches up with the model.
One other small note, as you continue to do this,
you might start to do things like 'my first model to explore NetLogo version 1'
and then when you make some major changes
you might want to save the new model as 'my-first-model-1' or '...-2',
this allows you to keep old versions that you have around,
of course you could also use a more sophisticated version control system
like svn or git or something like that as well.
That's a basic introduction to how to write code in NetLogo.
In the next talk we're going to discuss how to define properties within the code tab
that you can then use within your model.