Abu
Abu

Reputation: 19

Ask turtles to change colour and stop when they meet other turtles Netlogo

I have two turtles of colour 'green' and colour 'blue'. I would like when the green turtles meet the blue turtles, the blue turtles turn white and stop moving while the green turtles continue moving randomly. Here is my code, I did but it is gives an error of Expected a literal value and is not doing returning the expected results. Any assistance is greatly appreciated.

ask turtles with [color = green]
  [if any? other turtles-here with [color = blue]
    [set turtles with [color = blue] [color = white]
]
  ]

Upvotes: 0

Views: 244

Answers (1)

JenB
JenB

Reputation: 17678

You forgot to ask (instruct) the turtles. The keyword with subsets the correct turtles but you need to set a variable, not set a turtleset. Also, '=' is used for evaluation (comparing), not for assigning values. I think you want this:

ask turtles with [color = green]
[ if any? other turtles-here with [color = blue]
  [ ask turtles-here with [color = blue]
    [ set color white
    ]
  ]
]

Note that I had to write turtles-here again, or all the blue turtles would turn white. A more readable way that also reduces errors is to set up a temporary variable (using let) for the relevant turtles.

ask turtles with [color = green]
[ let changers other turtles-here with [color = blue]
  if any? changers
  [ ask changers
    [ set color white
    ]
  ]
]

Upvotes: 1

Related Questions