Doolcp
Doolcp

Reputation: 31

Using an if statement with an agentset in NetLogo

New to NetLogo. I am using the Fire Simple example, I added new tress with the pcolor=blue. I am trying to make a condition that the blue patch will only turn red if its 4 surrounding neighbors are red as well.

ask patches with [pcolor = blue] [ set pcolor red if (neighbors4 with [pcolor = red] > 3) ]

I get an error "> expected this input to be an agent or number or string, but got an agentset instead"

Upvotes: 2

Views: 293

Answers (1)

Jasper
Jasper

Reputation: 2790

So there are a couple small problems with your code as written.

Firstly, in NetLogo, the if command must be followed by a command block, per the dictionary entry. So you cannot do command if someCheck you have to do if someCheck [ command ]

Second, neighbors4 with [color = red] is going to give you an agent set, a collection of patches in this case. So you cannot compare an agent set with a number, like in neighbors4 with [pcolor = red] > 3. You need to get the count of the agent set: count neighbors4 with [pcolor = red] > 3

ask patches with [pcolor = blue] [ 
  if (count neighbors4 with [pcolor = red] > 3) [ 
    set pcolor red 
  ] 
]

Also, if we notice that the inner if statement is just an extra filter on which blue patches will turn red, we can actually use the with statement from the outer ask and simplify things a bit:

ask patches with [pcolor = blue and (count neighbors4 with [pcolor = red] > 3)] [
  set pcolor red 
]

Hope that helps.

Upvotes: 3

Related Questions