Hint (Today I learned...): Having turtles ask all patches (and probably the reverse)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Hint (Today I learned...): Having turtles ask all patches (and probably the reverse)

The entry for "ask" in the NL Dictionary notes that "Note: only the observer can ask all turtles or all patches." This helps avoid silly errors, but sometimes I hit a situation where turtles asking all patches to do something is the only way I can figure out how to do something. An example is having every turtle ask every patch to change a variable based on the patch's distance from the turtle.

 This probably reflects my poor NetLogo skills. But, in case others have found themselves in a similar situation, I've found a way to accomplish this:


 to setup
   ask n-of 10 patches [sprout 1]

 to go
   let the-patches patch-set patches ;; IMPORTANT LINE 1
   ask turtles [
     ask the-patches [ ;; IMPORTANT LINE 2
     set pcolor pcolor + distance myself

  If a turtle tried "ask patches", NL catches will object. However, a turtle can freely ask an arbitrary subset of patches just fine. Important line 1 creates a one such subset, a patch-set that happens to contain all of the patches.  However, it isn't caught by NL's debugger. In Important line 2, the turtles ask that subset to do something. Were the line written "ask patches", an error would appropriately be raised.

 Now, I fully recognize that I am deliberately short-circuiting a safety feature. In engineering, the technical term for doing so is "Stupid". So, I offer no guarantee that this won't destroy your program and cause your cat to go bald.

 Of course, if anyone knows a non-stupid way to accomplish the same thing, I'd love to hear it. I suspect a "foreach" loop might be part of the answer, but I kept failing when I tried it.