of

 

of is a primitive that allows us to reach into an agent and pull out the value of a variable it owns. Normally, agent variables (such as color, size, shape, as well as variables defined with <agents>-own commands) are accessed within an agent context using the ask primitive. of allows us to get to those variables outside of such a block or to get all the values for each agent in an agentset all at once as a list. For example, if we wanted to create model where each person picked a random friend and gave them an apple if they didn't already have one, we would write the following code:

ask people [
    let my-friend one-of other people
    if [apple] of my-friend = 0 [
        ask my-friend [ set apple apple + 1 ]
    ]
]

Things to keep in mind when using of:

In the model example below, we have a model that is similar to the popular online game agar.io. We have many turtles that represent circles and they move around randomly. When two turtles touch each other, the larger turtle eats the small turtle. We use of to compare the two touching turtles within an ask statement in order to pick the larger one and then to add the sizes of the two touching turtles.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

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

 
Published NetLogo models that use the of primitive:
 
 
Similar primitives:
with

Reports a subset of the original agentset that only contains the agents with specified characteristics.

Read more
to-report

Begin a procedure to create a custom reporter.

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: