set

 

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.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the set primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the set primitive:
 
 
Similar primitives:
let

Creates a local variable that only exists within a procedure or a statement surrounded with brackets (`[ ]`).

Read more
globals

Defines variables that can be accessed throughout the whole model and has the same value for all the agents.

Read more
turtles-own

Declare a variable that belongs to turtles.

Read more
patches-own

Defines custom characteristics (variables) for patches. Each custom characteristic can have a different value for each patch.

Read more
 
Learn another primitive: