SuuzK
SuuzK

Reputation: 1

Ticks loop in Netlogo

I'm making a model in Netlogo, and I now try to make a loop for the ticks. I actually want the turtles to walk to what I specified as 'forest' at some time steps, and going 'home' between other time steps. However, something is going wrong, and I really don't know how to solve the problem.

My code:

to go-home  
set mylist (list 120 240 360 480 600 720 840 960 1080 1200)
 let mylist2 [240 360 480 600 720 840 960 1080 1200 1320]
 ask turtles [if ticks > 80 and ticks <= 120 [facexy 0 35]]
  ask turtles [if ticks > ((one-of mylist) + 80) and ticks <= (one-of mylist2) [
       facexy 0 35]]
end

I also tried the loop function and the foreach function, but for both, the go function only works for one timestep, and then the program crashes.. (I suppose I'm doing something wrong)

Upvotes: 0

Views: 440

Answers (1)

JenB
JenB

Reputation: 17678

You almost certainly want a code block like:

to go-home
  if ticks > 80 and ticks <= 120 [ ask turtles [ facexy 0 35 ] ]
end

so that the ask is inside the if (which means the if only gets tested once rather than once for each turtle).

Your more general problem, however, is with how you are using the lists. The primitive one-of will randomly select one of the items from the list. So your code might select 720 from mylist and 480 from mylist2. It looks like you are wanting to switch every 120 ticks. If that is correct, then you don't need lists at all, you can simply use the mod operator with something like:

to go-home
  ifelse ticks mod 240 < 120
  [ ask turtles [ facexy 0 35 ] ]
  [ ask turtles [ facexy 0 0 ] ]
end

This will have them face patch 0 35 for 120 ticks (modifying the heading each tick if required) and then face patch 0 0 for the next 120 ticks and then 0 35 again etc.

Upvotes: 2

Related Questions