Variables (For a Printable Version Click Here) #### Variable Commands State Variables State variables are built in characteristics of turtles and patches. Turtles have 7 state variables: xcor ycor color heading breed shown? pendown? Patches have 3 state variables: xcor ycor patch-color (pc) The observer has no state variables. You can have access to a state variable by using its name in an expression. For example: xcor + 5 heading - 90 if pendown? [fd 1] All state variables, with the exception of the xcor and ycor of a patch, can be changed using the set command. For example: set color red set pc pc + 1 Note that the set command performs the same function whether it is separated by a space from the variable or not. Creating New Variables You can create new state variables for turtles, patches, and the observer. Whenever you create a new variable, StarLogo automatically creates a collection of new procedures to facilitate the use of the variable. For example, if you create the variable age for the turtles, then the turtles can use the procedure setage to change the value of the variable, and they can use the procedure age to access the value of the variable. You create new variables with the commands turtles-own, patches-own, and globals. You can type these commands in either the turtle procedures window or the observer procedures window (but not both), like this: turtles-own [age speed] Creates turtle variables age and speed patches-own [food] Creates patch variable food globals [clock] Creates global variable clock For turtle variables, the example age is used. For patch variables, the example food is used. And, for global variables, the example time is used. In order to use the commands listed below, you must first create the variables you want. Variable Suffixes You can access or set the state variable of another turtle or patch by using -at, -to , or -towards suffixes. For example: seth towards xcor-of 23 ycor-of 23 This command will set the heading of the caller toward turtle #23. set heading-at 2 3 90 This command will set the heading of all the turtles +2 patches away in the x direction and +3 units away in the y direction from the caller to 90 degrees. set pc-towards 270 4 green This command will set the patch color of the patch 270 degrees from the current heading and 4 patches away from the caller to green. -of takes a turtle ID number as its argument. -at takes a dx and a dy as its arguments, which are relative to the position of the caller. The observer is considered living on patch (0,0). -towards takes an angle in degrees and a distance in turtle steps. The angle is relative to the heading of the caller. Local Variables Like `globals`, local variables are not associated with any specific patch or turtle. However, the exist only within the procedure within which they are declared. For example: to check let [:num who + color] if :num > who * 2 [setc red] end or to check let [:num who + color] ifelse :num > who * 2 [setc red] [set :num who * color if :num > who * 2 [setc red]] end Parameters Parameters are like local variables, in that they only exist during the procedure they are associated with. They are "declared" when the caller of procedure gives them a value. You can pass more than one parameter to a procedure by separating the variable names with a space. For example: to move :number fd :number lt random 360 bk :number rt random 360 end to go move 30 end Here are a list of commands that affect variables:
A
age age-at age-of age-towards
B / C
clock
D / E / F
food food-at food-towards
G / H / I / J / K / L
let
M / N / O / P / Q / R / S
set setage setage-at setage-of setage-towards setclock setfood setfood-at setfood-towards
T / U / V / W / X /Y / Z