Adrian Lkop
Adrian Lkop

Reputation: 3

Is there a solution for Only the observer can ask the set of all turtles in Netlogo?

The code linked below doesn't work because when you run the code it tells you:

Only the observer can ASK the set of all turtles.
  error while turtle 35 running ASK
  called by procedure MOVE-TURTLES
  called by procedure GO
  called by Button 'go'

I've checked the code and I can't find a solution around it.

globals [marked-patches]
turtles-own [Angle lastAngle]

to setup
 clear-all
 create-turtles number [ setxy random-xcor random-ycor]
 ask turtles [
 set color red
 set size 1
 set Angle random-float 0.05]
 reset-ticks
end

to go
 ask patches [set pcolor yellow]
 ask turtles[
 move-turtles
 set lastAngle Angle
 set Angle random 360
 right Angle
 do-return-plot]
 do-count-patches
 if (count patches = marked-patches) [stop]
 tick
end

to plot-patch-count
 set-current-plot "Area Covered"
 set-current-plot-pen "Number of Patches"
 set marked-patches count patches with[pcolor = yellow]
 plot marked-patches
end

to do-return-plot
 set-current-plot "Return Plot"
 plotxy lastAngle Angle
end

to do-count-patches
  set marked-patches count patches with[pcolor = yellow]
  show marked-patches
end


to move-turtles
  ask turtles [
  rt random 360
  fd 1
  set pcolor yellow
  pen-down]
 ifelse show-travel-line? [pen-down][pen-up]
end

Upvotes: 0

Views: 553

Answers (1)

JenB
JenB

Reputation: 17678

That error is to stop you from making a very common mistake. Have a look at your go procedure - it hase ask turtles [ move-turtles ...], then the first thing you do in the move-turtles procedure is to ask turtles again. That is what the message is telling you, you have a turtle asking all the turtles to do something. If you have 10 turtles then each turtle will move 10 times because each turtle tells all the turtles to move.

In your case, you need to think about the order. Do you want one turtle to move, then to calculate the angle and so on, doing everything before the next turtle starts. If so, then leave the ask turtles in the go procedure and remove it from the move-turtles procedure. That will fix the message.

However, you also have some of your plotting in the go procedure and some in the move-turtles procedure. You may also want to think about how to break up the commands into procedures that each do one thing and one thing only. I find that it is easier for people who are new to NetLogo to have the go procedure just run through a list of procedures to call, and those procedures do the moving, plotting etc. Those procedures then start with ask turtles. The benefit of this approach is that the ask turtles is located in the code with the commands they are being asked to do.

Upvotes: 2

Related Questions