问题
I have a list which is storing the list of coordinates of patches.
let coor [ [0 1] [ 1 0] [1 -1] ]
Now I want to ask the patch at the location of the 2nd element of the list to change its color to red.
ask patch (item 1 coor) [ set pcolor red ]
This gives an error that patch expects a number instead of a list or block. How to make NetLogo understand that the (item 1 coor) is a set of two numbers?
Any other way to approach this? I have also tried using 'table' extension for this but there also same issue was coming.
回答1:
Just use an appropriate reporter:
to-report patch-at-xy [#xy]
let _x item 0 #xy
let _y item 1 #xy
report patch _x _y
end
This requires a tiny change in your code above:
to test
let coor [ [0 1] [ 1 0] [1 -1] ]
ask patch-at-xy (item 1 coor) [ set pcolor red ]
end
That said, you almost certainly should be storing a list of patches rather than of their coordinates.
来源:https://stackoverflow.com/questions/41276117/accessing-patch-through-coordinates-stored-in-a-list