Reputation: 13
What are the general rules to convert a code from patches to observer context?
For example, if I have a code like this, how should I modify it to convert the button from patches to observer context?
if numRed <= numYellow [set pcolor red]
if numYellow < numRed [set pcolor yellow]
if numGreen < numBlue [set pcolor green]
Upvotes: 0
Views: 297
Reputation: 17678
There are no general rules as such. But the most common way to change contexts is with the primitive ask
. Look at this code:
to testme
ask n-of 10 patches
[ set pcolor red
]
end
The procedure is in observer context. That is, it is written from the perspective of an outsider looking at the model. Then the ask
randomly selects 10 patches. The code switches perspective (at the open square bracket) and sort of pretends it's the first of those randomly selected patches. That patch sets its color (pcolor
) to red. Then the perspective switches to the second randomly selected patch. It also thinks to itself to change colour. While the code is running through those 10 patches, it is in patch context. The the close square bracket ends the code block, simultaneously ending the patch context and jumping back out to the observer context.
If your code is in patch context (or link context or turtle context), then you have to tell it which patches (or links or turtles) to be applying the code to. The default is all. Try this:
set pcolor red
Press the button and all patches will turn red.
I believe that a beginner in NetLogo should write every procedure from the observer context. This means that all the commands to patches and turtles are contained within clear ask <agentset> [ <do something > ]
structures. It is easier for you to keep track of what you are doing and which model entities are doing it.
Upvotes: 3