globals

 

globals is a primitive that we use to define custom global variables in NetLogo. A global variable is a variable that has the same value for all the agents in the model across all procedures. You can define your custom global variables by writing globals followed by brackets [].

globals [
    temperature
    oil-price
    usd-eur-exchange-rate
]

Once you define a global variable, you can then use the set primitive to change its value:

set usd-eur-exchange-rate 0.85
set temperature 36

And use its name to access its value in your code just like any other variable:

if oil-price < 1.5 and usd-eur-exchange-rate < 0.8 [
    buy-oil
]

Things to keep in mind when using globals:

In the model example below, we have some brown patches that represent the earth and some green patches that represent berries. We also have five turtles that represent the people who pick the berries. We use a global variable to keep track of the total number of the berries that are collected by all the people in the model. In the setup procedure, we set it to 0, and then in the go procedure, each time a person grabs a berry (turns a patch from green to brown), we increment the berries-picked global variable by one. We check the value of this global variable at each tick. If our pickers picked 60 or more berries, we stop the model. We also show the value of this variable through a monitor on the model's interface.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

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

 
Published NetLogo models that use the globals primitive:
 
 
Similar primitives:
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
set

Changes the value of a variable (global, local, or agent-owned).

Read more
let

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

Read more
 
Learn another primitive: