Trummler
Trummler

Reputation: 15

"You can't use TICK in a turtle context, because TICK is observer-only." - Don't know how to fix that error

I'm trying to create an own ecosystem and have already written about 100 lines of code. But I'm currently getting an annoying error message [Title] I cannot get rid of unless I delete the line with the «Tick» command.

That's how the Interface looks like at the moment

That's how the Interface looked like after the latest successful simulation

That's the defective Code:

turtles-own [saturation age gender]
breed [sheep a-sheep]
sheep-own [love-cooldown-sheep-f love-cooldown-sheep-m age%-sheep]
globals [tick-stopper]
patches-own [Next-to]

to setup
  clear-all
  setup-patches
  setup-sheep
  set tick-stopper 0
  reset-ticks
end

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

to setup-sheep
  create-sheep number_of_sheep [set saturation max-saturation-sheep set gender random 2 set shape "sheep" set age 0]
  if setup-sheep-random-age? [
    ask turtles with [shape = "sheep"] [set age random life-exp.-sheep]
  ]
  ask turtles with [shape = "sheep"] [
    setxy random-xcor random-ycor
    if gender = 0 [
      set color 118
    ]
    if gender = 1 [
      set color 98
    ]
  ]
end

to go
  if tick-stopper >= stop-at-tick and stop-at-tick? [
    set tick-stopper 0
    stop
  ]
  move-sheep
  sheep-eat-grass
  make-love-sheep
  check-death-sheep
  regrow-grass
  set tick-stopper tick-stopper + 1
  tick
end

to move-sheep
  ask turtles with [shape = "sheep"] [
    right random 60
    left random 60
    forward 1
    set saturation saturation - 1
    set age age + (((random life-exp.-sheep) + 1) / ((life-exp.-sheep + 1)/ 2))
    if gender = 0 and love-cooldown-sheep-f > 0 [
      set love-cooldown-sheep-f love-cooldown-sheep-f - 1
      set saturation (saturation - (saturation-loss-til-birth-sheep / contribution-period-sheep))
      ]
    ]
    if gender = 1 [
      if love-cooldown-sheep-m > 0 [
        set love-cooldown-sheep-m love-cooldown-sheep-m - 1
    ]
  ]
end

to sheep-eat-grass
  ask turtles with [shape = "sheep"] [
    if pcolor = green and saturation < max-saturation-sheep [
      set pcolor brown
      set saturation saturation + saturation-from-grass
    ]
    ifelse show-saturation?
      [ set label saturation ]
      [ set label "" ]
  ]
end

to make-love-sheep
  ask turtles with [shape = "sheep"] [
    if gender = 0 and love-cooldown-sheep-f = 1 [hatch 1 [set saturation birth-saturation-sheep set gender random 2 set age 0
      if gender = 0 [set color 118]
      if gender = 1 [set color 98]
    if gender = 1 and love-cooldown-sheep-m = 0 and saturation > saturation-for-love-sheep and 1 = count turtles with [shape = "sheep" and gender = 0 and love-cooldown-sheep-f = 0 and saturation > saturation-for-birth-sheep] in-radius 1 [
      set saturation saturation - saturation-loss-at-love-sheep
      set love-cooldown-sheep-m cooldown-for-love-sheep
      ask turtles with [shape = "sheep"] in-radius 1 [
        if gender = 0 and saturation > saturation-for-birth-sheep and love-cooldown-sheep-f = 0 [set love-cooldown-sheep-f contribution-period-sheep]
          ]
        ]
      ]
    ]
  ]
end

to check-death-sheep
  ask turtles with [shape = "sheep"] [
    set age%-sheep (100 * (age / life-exp.-sheep))
    if saturation <= 0 [die]
    if age%-sheep < 22.5039287 [
      if (10 ^(0.00984 *(age%-sheep ^ 2) - 0.25918 * age%-sheep + 2.922))/ life-exp.-sheep > random 10000 [die]
    ]
    if age%-sheep > 156.24733 [
      if 10000 / (life-exp.-sheep / 100) > random 10000 [die]
    ]
    if age%-sheep > 22.5039287 and age%-sheep < 156.24733 [
      if (10 ^(1.899801588 -(((16 / 75 * (age%-sheep ^ 3)) - (50 * (age%-sheep ^ 2))) / 131250)))/ life-exp.-sheep > (random 10000) [die]
    ]
  ]
end

to regrow-grass
  ask patches with [pcolor = brown] [
    set next-to count neighbors4 with [pcolor = green]
    if (random 1000000 / 10000) <= (grass-growth% * count neighbors4 with [pcolor = green]) [set pcolor green]
  ]
end

Thanks for your help <3 This is my first project with NetLogo 6.0.4 and it isn't planned to start any other projects after this one.

Upvotes: 1

Views: 1314

Answers (1)

JenB
JenB

Reputation: 17678

You have a bracketing problem in your move-sheep procedure. You have a structure like this:

to move-sheep
  ask turtles with [shape = "sheep"]
  [ right random 60
    ...
    if gender = 0 and love-cooldown-sheep-f > 0
    [ 
    ]
  ]
  if gender = 1
  [ if love-cooldown-sheep-m > 0
    [ set love-cooldown-sheep-m love-cooldown-sheep-m - 1
    ]
  ]
end

So you accidentally closed the ask turtles before dealing with the if gender = 1. As soon as you have if gender = 1 then it switches to turtle context.

Upvotes: 3

Related Questions