Command List

Procedures are a way to package up instructions for individual characters in StarLogo. Procedures begin with the word to followed by the name you wish to give the procedure. After the name, you may enter any number of procedure parameters. Each parameter must start with a : (colon) to indicate that it is a local variable. After pressing return, you then enter all of the instructions you wish to run when the procedure is called. Finally, a procedure finishes with the word end on a line by itself. Here is an example:

to wiggle
right random 40
left random 40
forward 1
end

In this procedure, named wiggle, we instruct a turtle to turn right a few degrees, then left a few degrees and finally move forward one step.

Here is an example of a procedure with one parameter:

to wiggle-a-little-bit :howmuch
right random :howmuch
left random :howmuch
forward 1
end

In this procedure, named wiggle-a-little-bit, we take a parameter named :howmuch, which we use like any other variable to control by how much the turtle should turn right and left. We can run a procedure that takes a set of parameters by writing the procedure's name followed by the values that we want the parameters to have (in left-to-right order):

to go
wiggle-a-little-bit 45
look-for-gold
end

In this procedure, named go, we invoke the wiggle-a-little-bit function with the value 45. 45 is assigned to the procedure parameter :howmuch, and then the procedure is run. Finally, when wiggle-a-little-bit finishes, we come back to where we left off in go, and then execute another procedure (not described here) called look-for-gold.

Note the difference between Turtle Procedures and Observer Procedures. Turtle procedures are instructions for individual turtles to follow, such as moving, or reproducing. Observer procedures are procedures that may change the environment or make changes which are beyond an individual turtle's control, such as the creation of new turtles or the setting up of the Patch Canvas. Think of the Observer as an omniscient being who can make changes to the StarLogo environment.

Observer procedures must be defined in the Observer Procedures Pane of the Control Center. Turtle procedures are defined in the Turtle Procedures Pane of the Control Center. If you put a procedure in the wrong place (such as putting wiggle into the observer procedures pane), you might get an error like observer doesn't know how to right in wiggle, indicating that the observer does not understand the command right used in the first line of wiggle.

While patches can run instructions, they cannot have procedures; there is no patch procedures pane. If you want to use patch commands, you must put them in an ask-patches [] command. Since ask-patches is itself an observer command, it should be written in the observer procedures pane.

One special Observer Procedure is the startup procedure. If you name one of your Observer Procedures startup, this procedure will automatically be run when you load the project.

Turtle, Observer, Patch

end

Description:
This command must be placed at the end of every procedure.

Notes:
Note, you can't put anything (even a comment) on the same line as end.

Related Commands:
output startup stop to
Observer

get-scheduler-random-seed

Description:
Returns the current value of the scheduler's random seed.

Notes:
Example: One use of get-scheduler-random-seed would be to have repeatable results. If you want to repeat the results of a specific trial, get the scheduler's random seed and simply set the random seed to the same number the next time you run the project.

Related Commands:
scheduler set-scheduler-random-seed switch-scheduler
Turtle, Observer, Patch

output something

Parameters:
something Anything the value to be returned

Description:
Exits the current procedure and returns something.

Examples:
output "hello" exits the current procedure and returns the string "hello".

Related Commands:
end loop stop to
Observer

scheduler

Description:
Reports the current scheduler, either 'fixed' or 'random'.

Related Commands:
get-scheduler-random-seed set-scheduler-random-seed switch-scheduler
Observer

set-scheduler-random-seed seed

Parameters:
seed Integer The seed for the random number generator

Description:
Sets the random seed of the thread scheduler to seed. Integers allowed are in the range -2^31 to 2^31-1.

This is not the same seed as used by the random command.

Notes:
Note: StarLogo uses the random generator provided by Java, which, as of now, is a linear congruential formula (Knuth Art of Computer Programming, Vol 2, Section 3.2.1.), which uses a 48-bit seed. StarLogo only allows you to set 32 bits of this seed.

Related Commands:
get-scheduler-random-seed scheduler switch-scheduler
Observer

startup

Description:
This is a special procedure name. Anything within the startup procedure will be executed each time the project is opened.

Examples:
to startup
clear-turtles ;save the patches
crt 100
ask-turtles [setc blue]
end

This process of clearing the turtles, and creating 100 new blue ones will occur everytime the project is opened.

Related Commands:
end to
Turtle, Observer, Patch

stop

Description:
Exits the current procedure immediately.

Examples:
to check-patch-color
if pc-ahead = red [rt 90 stop]
if pc-ahead = green [lt 90 stop]
fd 1
end

This procedure will cause turtles to turn right and then exit the procedure if the the patch ahead is red, turn left and exit the procedure if the patch ahead is green, and only move forward one if the patch ahead was not green or red.

Notes:
Note: stop does not stop any callers of the procedure, only the procedure itself.

Related Commands:
end loop output to
Turtle, Observer, Patch

stopall

Description:
Tells everything to stop running, including procedures, buttons, monitors, and command centers.

Related Commands:
stopbuttonname
Observer

switch-scheduler

Description:
Toggles back and forth between fixed order scheduling and randomized scheduling.

Related Commands:
get-scheduler-random-seed scheduler set-scheduler-random-seed
Turtle, Observer

to procedure-name

Parameters:
procedure-name Anything A single word which is the name of the procedure you wish to define

Description:
When you create a procedure, the first line of it must be to procedure-name where procedure-name is a one-word title for your procedure. procedure-name cannot be any command already recognized by StarLogo (this includes both built-in commands and the procedure names of other procedures you may already have written).

To add parameters to a procedure, write them after the procedure-name with a colon in front of them, like this:
to my-procedure :x :y :z

Inside the procedure body, you may use :x, :y and :z to refer to the values of the arguments passed in by the caller of the procedure.

All procedures must be ended with the word end on a line by itself (no comments may be on that line).

Procedures defined in the turtle procedures pane are only callable by turtles. Procedures defined in the observer procedures pane are only callable by the observer. Patches may not have procedures.

Examples:
to go
fd 1
rt 90
end

Now you may use go as a command in other parts of your program.

Related Commands:
end output startup stop