user3146700
user3146700

Reputation: 87

NetLogo: Error in avoiding walls and closed gates

I have this code in which turtles are suppose to change direction when they encounter walls and closed gates.

It is okay at the beginning but then it will give this message.

OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
error while turtle 259 running OF
  called by procedure GO
  called by Button 'Go'

I can send the whole model.

 if state = 1 [fd speed
       ifelse [pcolor] of patch-at-heading-and-distance 0 1 = black or [pcolor] of patch-at-heading-and-distance 0 1 = red
       [ lt random-float 90]
       [fd 1]  
  ; ifelse [pcolor] of patch-ahead 1 = blue
  ;    [ lt random-float 360 ]   ;; We see a blue patch in front of us. Turn a random amount.
   ;   [ fd 1 ]

        if (pxcor >= 89 and pxcor <= 90) and (pycor > 5 and pycor < 10) [facexy (87 + random 3) 25 fd speed]
        ;if (pxcor >= -10 and pxcor <= 1) and (pycor >= 6 and pycor <= 23 ) [facexy ( 7 + random 3) (26 + random 3) fd speed]
        if (pxcor >= 85 and pxcor <= 90) and (pycor = 26) [
          let choice random 2
          fd speed
          if choice = 1 [leftbench]
          if choice = 0 [facexy (87 + random 3) 76]
        ]
        if (pxcor >= 83 and pxcor <= 92) and (pycor >= 75 and pycor <= 77) [rightbench fd speed]
        if pcolor = brown and not any? other turtles-here
        [ move-to patch-here
          set seated? true
          set pcolor orange
          ]
      ] 

Upvotes: 1

Views: 517

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30453

Does your model have wrapping at the world edges turned off in one or both directions?

If so, then your code has to handle the possibility that sometimes this patch:

patch-at-heading-and-distance 0 1

won't exist, if the turtle is near a world edge. If that happens, then patch-at-heading-at-distance will return nobody, and when you try and do:

[pcolor] of patch-at-heading-and-distance 0 1 = black

you get the error, because no pcolor can be retrieved from a nonexistent patch.

The typical way to code around this is to do something like:

let target patch-at-heading-and-distance 0 1
if is-patch? target and ...    

Finally, I wonder whether you really mean patch-at-heading-and-distance 0 1. It's equivalent to patch-at 0 1, which refers to the patch north of the turtle; not sure why you'd want that. If you want to test the patch ahead of the turtle (the patch the turtle will land in if it uses fd), use patch-ahead.

Upvotes: 2

Related Questions