AKarr
AKarr

Reputation: 183

How should i modify this code to allow me to let turtles turn randomly down paths at a junction?

I have created a new patch type which allows turtles to turn randomly down various paths while one a junction patch ( with pcolor 6 ). How should I modify this code so that I do not get the error "Towards expected input to be an agent but got nobody instead." The code is as follows :

if pcolor = 6 [ set heading towards  one-of neighbors in-cone 1 180]

Any help would be much appreciated.

Upvotes: 0

Views: 89

Answers (2)

Arthur Hjorth
Arthur Hjorth

Reputation: 889

To specifically answer your question, you need to check if there are any patches that fulfill your criteria. To do that, you can do

ask a-turtle [if any? neighbors in-cone 1 180 [face one-of neighbors in-cone 1 180]]

Doing it this way, you will create the same turtle set twice (when you check if there are any, and before you face one of them), so a more optimized way of doing this is:

ask a-turtle [
  let eligible-neighbors neighbors in-cone 1 180
  if any? eligible-neighbors [face one-of eligible-neighbors]
]

That said, I think Alan is right that you are getting this error because you have wrapping off and your turtles are either in a corner or facing a wall. If this is the case, you need to figure out what to do in that case. If you just want them to turn around and keep going, you could use ifelse like this:

ask a-turtle [
  let eligible-neighbors neighbors in-cone 1 180
  ifelse any? eligible-neighbors 
    [face one-of eligible-neighbors] ;; face a neighboring patch if there are any
    [rt 180] ;; else, turn around 180 degrees
]

Upvotes: 1

Tom Czaczkes
Tom Czaczkes

Reputation: 1

try first making a temporary variable with 'let', then setting the heading towards that.

e.g.

let FaceHere one-of neighbors in-cone 1 180
face FaceHere

haven't tried that - just an idea.

by the way, you can replace

set heading towards

with

 face

Upvotes: 0

Related Questions