3

I need help in Netlogo: when a turtle interacts witch a patch where it is currently placed on and the patch-variable (which is a string) decides if the turtles variable (which is an integer) grows. For example:

turtles-own [weight]
patches-own [food]

to setup
  clear-all
  create-turtles 1 [
    setxy random-xcor random-ycor
  ]
  ask patches[
    if pxcor >= 0 [set pcolor green 
      set food "Fries" ]
   
     if pxcor < 0 [set pcolor blue
      set food "Burger" ] 
  ]
  reset-ticks
end

to go
  increase-weight
  walk-around
  tick
end

to walk-around
  ask turtles [
    right random 120 - 60 
    fd 0.1
  ]
end

to increase-weight
  ask turtles [
    if food != "fries" [
      set weight (weight + 1)]
    if food != "burger" and  [
      set weight (weight  + 10)]
  ]
end

The problem is the turles weight goes up by 11 not with the value of 1 OR 10. I guess its something with patch-here !? But i cant get it running.

Thanks a lot

Goku
  • 31
  • 3

1 Answers1

2

You were close. The Major issue was the differences in strings. "Fries" and "fries" are not exactly equal. Change one to match the other. While the line if food = "Fries" will work I would recommend using the patch-here in an if statement with the name of the patch variable in brackets like this:

  to increase-weight
    ask turtles 
    [
      if [food] of patch-here = "Fries" 
        [set weight (weight + 1)]
      if [food] of patch-here = "Burger" 
        [set weight (weight  + 10)]
      set label weight
    ]
  end

That will make it super clear where the food variable is coming from.

I was confused by your use of the != and what type of food you thought the turtle was on. The above code increments weight by 1 for fries and by 10 for a burger. Your original code with your != though inverted that, giving +10 for fries and +1 for burger. The use of the != did account for your increase by 11 (10 and 1) because it was always not on either of the patches.

References:

AWoods
  • 98
  • 8
  • Thank you for your help. I see i got it wrong with !=. But now when I watch a moving turtles weight it doesnt even increase at all. Its all the time 0. As if there is no interaction between turtle and patch. – Goku Oct 23 '22 at 11:48
  • 1
    Don't forget that when you inspect a Turtle, and then hit setup, the turtle you were inspecting dies!, OHNO! But the inspection Window stays open, looking at the dead turtle, while the new setup turtle is off and running. It happened a couple of time to me too while I was testing. You have to close the inspection window, hit setup, and then select the newly made turtle and inspect to see the results. – AWoods Oct 23 '22 at 16:03
  • hhhmm nevertheless the weight stays zero. Did it work for you? observer> ask turtles [show weight] always shows (turtle 0): 0 – Goku Oct 23 '22 at 19:40
  • 1
    Yes it worked for me. Could it Be a capital F and capitol B? In my Code I had Lower Case? I modified my answer to have Capitol F and B like your code. – AWoods Oct 23 '22 at 20:32
  • 1
    Adding the line `set label weight` after the if statements in the increase-weight command helps to show the weight and see it increase with out using the inspect turtle. – AWoods Oct 23 '22 at 20:40
  • It works now. Thank you for your time and effort. I really appreciate your help. – Goku Oct 23 '22 at 21:09
  • 3
    @AWoods While everything you say is correct and your code works, that is because of the capitalisation issue and the use of `=` instead of `!=`. The `[food] of patch-here` construction is not strictly necessary for it to work, since a turtle can already access the parameters of any patch it is on. As such, `if food = "Fries" []` would already suffice. It can still help provide clarity though. – LeirsW Oct 24 '22 at 11:36
  • Thank you @LeirsW for the clarification. It is Nice to know that the turtles can access that patch variable directly. I think I would still choose to access it through the `of patch-here` so that I am super clear about where the variable is located that I am accessing. – AWoods Oct 25 '22 at 00:52
  • 1
    @AWoods I agree that it is good practice, and is clearer, but still I would suggest you edit your answer. Currently you are giving incorrect advice about what the issue was. Definitely include it in your answer as a tip, just not as a the solution to the issue. – LeirsW Oct 25 '22 at 18:17
  • 1
    @LeirsW updated as you suggested! – AWoods Oct 26 '22 at 19:41