Goku
Goku

Reputation: 31

Netlogo - Turtle-Patch interaction with string-variable

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

Upvotes: 3

Views: 114

Answers (1)

AWoods
AWoods

Reputation: 346

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:

Upvotes: 2

Related Questions