user3724079
user3724079

Reputation: 55

Change color based on pcolor - NetLogo

I am trying to create a function in NetLogo that will allow me to use clickable "color swatches" to determine the color of a turtle. Here is my code so far:

 if mouse-down?
 [ ask patch mouse-xcor mouse-ycor
   [ if pcolor != 0 mod 10
     [ show pcolor 
       ask turtle whichturtle 
        [ set color pcolor]
     ]
   ]
 ]

(I have created a drop-down with the global variable which allows the user to select which turtle they would like to change.)

However, when I run this code, the specified turtle changes its color to the color of the patch it's currently standing on, rather than that of the patch the user clicked on.

How would I fix this problem? Any help is much appreciated.

Upvotes: 2

Views: 441

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

You need to use myself to refer the patch that's asking the turtle to change its color:

ask turtle whichturtle [
  set color [pcolor] of myself
]

It can get confusing, sometimes: learning to keep track of the context that you are doing things in is an important part of mastering NetLogo...

One way to avoid getting lost in the different levels of asking is to assign things to local variables at a place in the code execution where you know exactly where you are. In your case, it could have been something like:

if mouse-down? [
  ask patch mouse-xcor mouse-ycor [
    if pcolor != 0 mod 10 [
      let chosen-color pcolor 
      ask turtle whichturtle [
        set color chosen-color
      ]
    ]
  ]
]

Upvotes: 3

Related Questions