Akari Tsukada
Akari Tsukada

Reputation: 27

Only observer can ask all turtles - NetLogo error

I am new user for Netlogo, Although when I checked the code, there are no issues. However, when i run the program a while, it throws error message

Only the observer can ASK the set of all turtles. Error while turtle 0 running ASK. called by procedure EXIT called by procedure Customer, GO and by button 'go'

to go
customer
end

to setup-turtles
create-turtles 1
ask turtles
[
set shape "person"
set size 3
set heading -90
fd 10
setxy 15 -15
set color red
]
end

to customer
ask turtles
[
set products ( products )
rt (random 360)
fd 1
if patch-here = one-of patches with
[
  pcolor = green
]
[
  set pcolor orange
  set products (products + 1)
]
if patch-here = one-of patches with
[
  pcolor = gray
]
[exit]
show count patches with
[pcolor = green ]


move-to one-of patches with
[
  pcolor = black
]

]
end



to exit
ask turtles
[
ifelse patch-here = one-of patches with
[
  pcolor = gray
]
[ifelse count products >= 2
  [
    die
  ]
  [move-to one-of patches with
    [
      pcolor = green or pcolor = black
    ]
  ]
]
  [
    die
  ]
move-to one-of patches with
  [pcolor = green
  ]

move-to one-of patches with
    [pcolor = black
    ]


  ]

end

Upvotes: 1

Views: 1676

Answers (1)

JenB
JenB

Reputation: 17678

In your customer procedure, you have

ask turtles
[ ...
  if patch-here = one-of patches with [pcolor = gray]
  [ exit ]
  ...
]

So the exit procedure is being called by any turtle that is on a gray patch. Each turtle that meets that condition enters the exit procedure. As soon as it enters that procedure, the first command (in the exit procedure) is to ask turtles. So a turtle is asking all turtles to do something.

This is explicitly banned by the NetLogo language partly because it is a common source of beginner errors and is generally both unnecessary and inefficient. You have already selected the turtle to exit, what is it that this particular turtle needs to do to actually exit. It is very unlikely that they need to identify all the turtles on gray patches.

Upvotes: 2

Related Questions