NetLogo - how to give each patch a unique identity / poster / name
I wanted to have a field with many different patches that have some attributes (like a random (but fixed) amount of food) as well as a name or ID. That is, the top left patch should have the name "1" (or whatever is ever possible), but cannot share that name with another!
Thanks for your help, I couldn't find anything.
source to share
The patch coordinates already act as a unique identifier. Thus, it patch 7 10
applies to patch pxcor
7 and pycor 10
.
However, if you want a single integer id, you can do something like:
patches-own [ id ]
...
(foreach (sort patches) (n-values count patches [?]) [
ask ?1 [ set id ?2 ]
])
sort patches
creates a list of patches with the first top-left patch and continued from left to right, top to bottom.
n-values count patches [?]
creates a list of numbers from 0 to count patches - 1
.
source to share
Regarding assigning a "unique identifier" to each patch, my advice ... don't do that. The patches are already uniquely identified by the combination of them pxcor
and pycor
. Therefore, if you want to access the top left patch, you can refer to it as patch -16 16
.
You should also be aware that patch references (and turtles and references) can be stored directly in variables. Therefore, any time you plan to store a "patch ID", you must keep a link to the patch. For example, if you want to store the top left patch in a global variable:
globals [ top-left-patch ]
to setup
set top-left-patch patch min-pxcor max-pxcor
end
( min-pxcor
and there min-pycor
will be a report -16
and 16
, or whatever fits your world dimension.)
And then, later, you can link directly to the saved patch:
ask top-left-patch [ set pcolor red ]
But if you really want to create an id, nonetheless, Brian's answer is the way to go.
source to share