Rita Maestri
Rita Maestri

Reputation: 23

Setting turtle's variable from nested ask

I am new to netlogo so my question is maybe silly. I want to set the variable conv as true for two or more turtles together if they face each other. So I loop over all the turtles and ask them if there is a turtle in their cone of view. If there are some, I ask to those turtles if conv is false for them and if myself is in their cone of view. If this is the case, I need to set conv true for both the turtles that are facing each other. The code below obviously doesn't work, but I don't know how to write it differently.

ask turtles[
ask other turtles in-cone 4 90[
    if (not conv) and (member? myself other turtles in-cone 4 90)[
        set conv true
        set [conv] of myself true]
    ]
]

Upvotes: 0

Views: 194

Answers (1)

JenB
JenB

Reputation: 17678

The keyword set instructs a turtle to set its own variable (or a global variable) to the specified value. That means you need to change to the perspective of the turtle that you want to change the variable for. Here is a complete model that does that changing of perspective.

to testme
  clear-all
  create-turtles 100
  [ setxy random-xcor random-ycor
    set color blue
  ]
  ask turtles
  [ ask other turtles in-cone 4 90
    [ if member? myself other turtles in-cone 4 90
      [ set color red 
        ask myself [ set color red ]
      ]
    ]
  ]
end

Basically, instead of set [conv] of myself true] you will need something like ask myself [set conv true].

Upvotes: 1

Related Questions