Kilian Murphy
Kilian Murphy

Reputation: 335

you cant use tick in a turtle context because tick is observer only!! NETLOGO

If i add if to my go command i get the error message

you cant use tick in a turtle context because tick is observer only

here is my go commands. search eat go-home and den are all defined in my commands.

energy is also defined as a global variable that turtles own

to go

  if ticks = day-length  [set day day + 1 create-next-day]


  ask adults [search eat]
  if energy < 20000 [ask adults [go-home den]]

 tick 

end

if i take out the line

if energy < 20000 [ask adults [go-home den]]

it runs perfectly, but i need that line or an equivalent. please help

Commands
;;-------------------------------------------------------------;;
;;------------------- ADULTS COMMANDS--------------------------;;
;;-------------------------------------------------------------;;

;; Need to add a private variable (wolves own) for wolves [state] and then need to code 4 states 1. Den 2. Search 3. Eat 4. Return
;; need to code all 4 states
;; Need to correctly allocate energy and the state of decline

To den ;when wolf is full
  set energy  energy  - .04
end


to search ;when wolf is hungry
  set energy  energy  - .07
    fd v-wolf
   if random 600 = 1 ;; frequency of turn
  [ ifelse random 2 = 0 ;; 50:50 chance of left or right
    [ rt 15 ] ;; could add some variation to this with random-normal 45 5
    [ lt 15 ]] ;; so that it samples from a dist with mean 45 SD 5

  ;; check if it can see a prey/food item
  ;; here i think we probably pick one of several possible prey
  ;; that are detectable randomly using the one-of command.
  ;; We should probably select the nearest one instead ** The turtles are getting
  ;; caught between two prey species and dying because they cant choose which one **
  if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
  if energy < 0 [die]

end


To eat ;to kill prey and eat it
  let kill one-of prey-here in-radius smell
  ;need to code in a variable for success too
  if kill != nobody
    [ask kill [ die ]
      set energy energy + 10000]
end



to go-home ;to head home after they've eaten and den until they need to feed again
 if energy > 30000 [set target-patch min-one-of (patches  with [pcolor = white]) [distance myself]]
  face target-patch
  fd v-wolf
  set energy  energy  - 1
end

Upvotes: 2

Views: 6377

Answers (2)

Alan
Alan

Reputation: 9620

if energy < 20000 [ask adults [go-home den]] will be a problem in go if energy is (as it appears) a turtle variable. This will make the context of the procedure a turtle context, not an observer context.

Edit:

For example, if energy is a turtle variable, perhaps you meant

ask adults [if (energy < 20000) [go-home den]]

Upvotes: 3

JenB
JenB

Reputation: 17678

First, you need to build your code much more gradually. You have multiple parts of your code that don't work and that you don't understand. Try adding the smallest amount you can and make sure that works before adding anything else. You have three different questions at the moment, with errors in different parts of your code.

On the context issue in Alan's answer, think about it this way: the variable 'energy' belongs to turtles. This means that if you have 10 turtles, you have 10 variables named 'energy', one for each turtle. Which one are you checking whether it is <20000?

What you probably want is to check it for EACH turtle individually and get the turtle to do the required action if it passes the test. So it must be inside ask turtles [], and that changes from the observer to the turtle context (what model entity is doing the thing).

to go
  if ticks = day-length
  [ set day day + 1
    create-next-day
  ]

  ask adults
  [ search
    eat
    if energy < 20000
    [ go-home
      den
    ]
  ]

  tick 
end

I have also cleaned up your formatting. This is not a requirement, NetLogo is happy to deal with spaces wherever you put them. However, as your code gets longer and more complicated, it will be much easier for you to debug if you follow some basic practices (1) each call to a procedure, each command etc on a separate line (2) bracket [] and indent so that you can see the code block that the brackets enclose.

Upvotes: 1

Related Questions