0

I want the turtles to back again to their areas if batches which I had defined before. The problem is I got an error

you can not use my_ area in a patch context, it is a turtle-only 

the button is

to back_from_event

  ask turtles with [ area = 1 ]  [ move-to one-of patches with [ (not any? other turtles-here) and ( area = my_area )  ] ]

end

the patches defined as below and the turtles were in areas (2,3,4,5) and moved to area 1 and I need them to back again to there areas:

to define_areas

  ask patches with [ (pxcor > -3) and (pxcor < 3) and (pycor > -3) and (pycor < 3) ] [  set pcolor white set area 1    ]
  ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor > 4) and (pycor < 16) ] [  set pcolor white set area 2     ]
  ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor > 4) and (pycor < 16) ] [  set pcolor white set area 3    ]
   ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor < -4) and (pycor > -16) ] [  set pcolor white set area 4    ]
  ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor < -4) and (pycor > -16) ] [  set pcolor white set area 5   ]

end
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • please also provide your `turtles-own` and `patches-own` statements – JenB May 31 '20 at 22:48
  • Sure patches-own [ area ] turtles-own [ my_area culture child_2_color child_1_color ] and the setup is : to setup ca define_areas create_parent create_children ask turtles [ set my_area [area] of patch-here ] ask turtles [ set culture [] ] repeat cultural_features [ ask turtles [ set culture fput ( random traits_per_feature + 1 ) culture ] ] reset-ticks end – Mohamed Salah Jun 01 '20 at 11:31
  • You should edit your question, not put code in comments. Look at your comment and the code is clearly unreadable. – JenB Jun 01 '20 at 13:11

1 Answers1

1

Okay, this is the same problem as your previous question. Please try to understand the answer rather than simply copy the code. Otherwise, you will keep asking the same question.

You have 'area' as a patch variable and 'my_area' as a turtle variable.

What you need to realise is that turtle to patch is unique because a turtle can only be in one place at a time. Therefore, a turtle can access the variables owned by the patch that it is sitting on without having to specify the patch. So code like this is okay:

ask turtles with [area = 1] [ ]

This is because it is equivalent to:

ask turtles with [ [area] of patch-here = 1] [ ]

However, a patch cannot access a variable owned by a turtle because there may be multiple turtles on the same patch. For example, if you asked a patch to set its pcolor to color and you had red turtle and a blue turtle on the patch, how would it know which color to choose?

Your error says "you can not use my_area in a patch context, it is a turtle-only". That is telling you that you tried to have a patch use the my_area variable but that variable is owned by turtles. So you didn't tell it which turtle to get my_area from.

This is what you have:

to back_from_event
  ask turtles with [ area = 1 ]
  [ move-to one-of patches with [(not any? other turtles-here) and (area = my_area)] 
  ]
end

I assume you want the area of the patch to be the same as the my_area of the turtle doing the asking. That is what myself is for.

to back_from_event
  ask turtles with [ area = 1 ]
  [ move-to one-of patches with [(not any? other turtles-here) and (area = [my_area] of myself)] 
  ]
end
JenB
  • 17,620
  • 2
  • 17
  • 45