Reputation: 881
When I run the following two lines I get different answers. Does anybody know why? The first gives the answer I want:
ask turtles[
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = tempcol]]
ask turtles[
set nextcolor [color] of self
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = [color] of self]]
Upvotes: 0
Views: 68
Reputation: 10301
You're right in that the issue is with the use of self
- from the dictionary entry for that primitive:
"self" is simple; it means "me". "myself" means "the agent who asked me to do what I'm doing right now.
In short, you want myself
in the second example. Currently, your second example is saying something like, "turtles, show the count of neighbor turtles whose color is the color of themselves" where you really want to say "turtles, show the count of neighbor turtles whose color is the color of myself." For a potentially clearer example, check out this setup:
to setup
ca
crt 10 [
set color red
setxy random-xcor random-ycor
]
ask n-of 3 turtles [
set color blue
]
reset-ticks
end
This creates 7 red turtles and 3 blue turtles. Now, if you ask one of the blue turtles to show the count of of turtles with the same color as itself, we should expect it to return a value of 3. If you run that code using self
, however, the value returned is 10- because all turtles have a color that is equal to the color of themselves:
to self-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of self' example:"
show count turtles with [ color = [color] of self ]
]
end
If you run the exact same code but use myself
, it returns the answer we would expect:
to myself-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of myself' example:"
show count turtles with [ color = [color] of myself ]
]
end
I would also point out that almost all of your of self
statements are redundant-you should be able to take them all out (except for [color = [color] of self]]
that you will be changing to a myself
statement anyway) and have your code run as before.
Upvotes: 2