Set changes the value of a variable to the provided new value. We can use set to change the values of global variables created with globals, local variables created with let, predetermined agent characteristics (e.g., pcolor, size, xcor), and custom agent characteristics created with turtles-own, patches-own, and links-own. For example, if we wanted to assign a random age to each turtle and make older turtles blue, we would write the following code:
turtles-own [age]
to setup
clear-all
create-turtles 100 [
set color green
set age random 80
if age > 65 [
set color blue
]
]
reset-ticks
end
In the model example below, we have a garden with some brown patches that represent the areas on which berry plants grow. Our farmer moves around randomly and picks the berries. Each plant has a different amount of berry. We use set for various goals in this model. First, we use set to change agent characteristics such as the shape of the farmer, the pcolor of the patches, and the shape of the plants. Then, we use set to count the total amount of berries that our farmer collects.
Once you mastered the set primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
set primitive:letCreates a local variable that only exists within a procedure or a statement surrounded with brackets (`[ ]`).
globalsDefines variables that can be accessed throughout the whole model and has the same value for all the agents.
turtles-ownDeclare a variable that belongs to turtles.
patches-ownDefines custom characteristics (variables) for patches. Each custom characteristic can have a different value for each patch.