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
:
globals
primitive. globals [1st-offer]
.let
primitive instead.turtles-own
, patches-own
,links-own
.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.
Once you mastered the globals
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
globals
primitive:turtles-own
Declare a variable that belongs to turtles.
patches-own
Defines custom characteristics (variables) for patches. Each custom characteristic can have a different value for each patch.