A procedure, in StarLogo, contains a set of instructions or commands that can be called within the program. Using procedures make executing commands often easier to do. This tutorial will show you how to crate a simle procedure that makes agents in Spaceland move randomly.
The first step to creating a procedure is to drag out a "Procedure" block onto the canvas.
1. Click the Procedure tab in the Factory palette.
2. Click on the "Procedure" block and drag it onto the canvas. For this tutorial, we will place it on the Turtles' page.
Once the block is placed on the canvas, you will notice a change to the block. The name of the page on the canvas will appear at the top of the block and the name of the procedure at the bottom. Right now, our block is called "Procedure". We can change its name by clicking on it and typing a new one. Let's call this procedure "Movement".
We want this procedure to make turtle agents move randomly, so we will need some movement blocks to do this.
1. Click on the Movement tab in the Factory palette.
2. Drag out "forward", "left", and "right" blocks onto the canvas and connect one block after the other under the procedure block.
As you see, once these specific blocks are placed on the canvas, they come with number blocks as well.
1. Click the Math tab and drag out two "random" blocks. The random blocks, automatically come with a number block, but we will not need them for this procedure so we can delete the number blocks by dragging them to the trashcan or selecting them and pressing the delete button on your keyboard.
2. Move the number blocks attached to the "left" and "right" movement blocks and reattach them to the random blocks. Now attach the new random blocks to the two movement blocks.
Now our procedure is complete and we can now use it.
Most procedures are directly used in runtime blocks. However, they can be also called anywhere, even within another procedure. For this tutorial, we will take the simple approach and call our procedure in a runtime block.
1. Click the Setup and Run tab in the Factory palette.
2. To run a procedure in "real time", we have the choice of using either a "Forever" block or a "Run" block. For this tutorial, we will use a "Forever" block so drag one out and place it in the runtime page of the canvas.
When a procedure is created, a new block is also created with the same name of the procedure. This block can be used to call that procedure in any place we specify.
1. To find this new call-block we will go to the "My Blocks" palette by clicking the right arrow next to Factory.
2. Click on the Turtles tab and find the block labeled "Movement". This is the call-block.
3. Drag the call-block onto the canvas and attach it to the Forever block underneath the Turtles hook.
When a runtime block is placed on the canvas, a new block appears in the runtime space of Spaceland. Now that we have attached a procedure to our Forever block, we can run it. All we have to do is simply click "forever" and the preset turtles will roam around the terrain!
Now that you've learned to define a specific procedure, here is a general rule that can be followed for making other procedures.
1. Define the procedure. ("Procedure" tab => Procedure block)
2. Call the procedure. (My Blocks palette => drawer of page where procedure is defined)
Then you are done and are free to play around with procedures!