palebluedot
palebluedot

Reputation: 53

Ask turtles to move to patch that is their state variable

In my model, I have turtles that start at a random location. This location is then saved as a state variable home-xy using the patch-here command. It is thus stored as format (patch 234 345). I then want my turtles to return to this location at the end of a procedure. I've tried the following two pieces of code:

ask turtles [
   move-to home-xy
]

ask turtles [
   let x [pxcor] of home-xy
   let y [pycor] of home-xy   
   move-to patch x y 
]

These do not work, but I think they represent my problem well enough. Thank you.

Upvotes: 1

Views: 598

Answers (1)

JenB
JenB

Reputation: 17678

Not sure why your first code didn't work. I just tested this and it's fine (returns to the centre of the patch where it started):

turtles-own [home-xy]

to setup
  clear-all
  create-turtles 20
  [ setxy random-xcor random-ycor
    set home-xy patch-here ]
  reset-ticks
end

to go
  ask turtles [ setxy random-xcor random-ycor ]
end

to go-home
  ask turtles [ move-to home-xy ]
end

You might want to inspect a turtle and make sure that home-xy is being set properly.

Upvotes: 2

Related Questions