Shelby Patrick
Shelby Patrick

Reputation: 19

Netlogo error message: you can't use tick in a turtle context because tick is observer only?

I am trying to write a model where a hunter goes out to a specific target, has a certain probability of actually killing the target animal, and if they do kill the animal then a certain amount of resources are added. I am trying to use a slider for the probability of hunting success so that I can run multiple trials in BehaviorSpace with different success probabilities.

I've tried to add the code about hunting success rate and adding a differing amount of resources both as part of the procedure where the hunters move to the target animal and then hunt or as a separate procedure where they move to the animal and then a separate procedure dictates the success rate and how many resources they acquire. When the procedures are separate I get the tick is observer-only error. When the procedures are together, the hunters then just move one step forward and back without ever actually going to the target. I'm really not sure what do with this - any help is appreciated! My full code is below.

breed [people person]
breed [seals seal]

people-own [
  resources
  hunt-done?
  target

]

to setup
  clear-all
  setup-seals
  setup-people
  setup-patches
  create-hazards
  reset-ticks
end


to go
  go-to-target
  encounter-hazards
  hunt
  return-home
  tick
end


;; turtle procedures

to setup-people
  create-people 4 
  ask people [
    set shape "person"
    set size 5
    setxy 0 0
    set resources 0
    set hunt-done? false
    set target one-of seals
  ]
end

to setup-seals
  create-seals 10
  ask seals [
    set shape "fish"
    set size 5
    setxy random-xcor random-ycor
  ]
end
    
  
to go-to-target
  ask people [
    ifelse distance target > 1 [
      face target
      forward 1
    ][
      move-to target ]
  ]
end

to hunt
  ask people-on target [
    ifelse random 100 > hunting-success [
          set resources resources + (13 + random 7)
          set hunt-done? true
    ][
      set resources resources + 0
      set hunt-done? true
    ]
  ]
end


to encounter-hazards
  ask people [
    if pcolor = red [
      set hunt-done? true
     move-to patch 0 0 
    ]
  ] 
end
   
to return-home
  ask people [
  if hunt-done? = true [
     move-to patch 0 0
    ]
  ]

end
    
;; patch procedures

to setup-patches
  ask patches [
    set pcolor gray
  ]
end

to create-hazards
  ask n-of 20 patches [set pcolor red]
end

;; reporter procedures

to-report people-final-resources
  report sum [resources] of people
end

Upvotes: 0

Views: 109

Answers (1)

LeirsW
LeirsW

Reputation: 2305

The problem is in your hunt procedure, more specifically in ask people-on target.

target is a turtle-own variable. That means that the procedure can't just be run by the observer. After all, the observer doesn't know which of the turtle's targets you are referring to. From this follows that hunt is a turtle-context procedure. And since hunt is a turtle-context procedure, go should also be turtle-context.

You need to move checking which turtles are at their target out of the observer context and into the turtle context. The following is an example:

to hunt
  ask people [ if [patch-here] of target = patch-here [
     <...>
     ] 
  ]
 end

Upvotes: 0

Related Questions