Reputation: 418
How can I set timer counting for multiple turtles at once? Below is the sample syntax. With this syntax, if two turtles are present, timer counting will be cumulatively counted by 2 for each tick. Example: 2, 4, 6, 8, ..., another example: If 3 turtles are present, 3, 6, 9, ..., cumulative counting is done for each tick. The decrement timer also has the same problem. In this model, a turtle infinitely born at the origin patch (0 0), and after a certain period of time, the turtles die one by one. This problem occurred even if there were multiple patches, too. I probably need your advice. Thank you.
ask (turtles-on patch 0 0)
[
set count-up count-up + 1
]
Upvotes: 0
Views: 740
Reputation: 17678
I think you are confused about the difference between a global variable and a turtle owned variable. If you have multiple turtles being created at different times, then you probably want each to have its own timer (so that the value can be different for different turtles). If all you want is a global variable to increase or decrease each tick, then don't put it in an ask
statement. Just say something like 'set counter counter + 1`.
Here is a complete model that should help you to understand what's going on in your code.
globals [num-turtles counter ave-count-up]
turtles-own [count-up]
to setup
clear-all
set num-turtles 5
reset-ticks
end
to go
set counter counter + 1
if count turtles < num-turtles
[ ask patch 0 0
[ sprout 1
[ set count-up 0 ]
]
]
ask (turtles-on patch 0 0)
[ set count-up count-up + 1
show count-up
]
type "Current value of counter is " print counter
type "Number of turtles: " print count turtles
type "Total of all turtle counters: " print sum [count-up] of turtles
set ave-count-up mean [count-up] of turtles
tick
end
Upvotes: 2