Setting the age on turtles
I have my turtles age
as one of their variables and have age
a tick set so that my turtles age when they reach a certain number of ticks. However, this causes all turtles to age at the same time (say when ticks = 5), regardless of when they were created.
Is there a way to get the age when the turtle is created? So, if the turtle is created at the 5 mark, its age starts at zero, but is still the same length as 1 tick?
Yes! I'm sorry! Here is the code I was playing with before I put it into my actual model.
breed [kids kid]
breed [adults adult]
breed [elderlies elderly]
turtles-own [age
z]
to setup
clear-all
create-kids 10
[setxy random-xcor random-ycor]
set-default-shape kids "fish"
create-adults 10
[setxy random-xcor random-ycor]
set-default-shape adults "person"
set-default-shape elderlies "cow"
clear-output
reset-ticks
end
to go
ask kids [birthday
move]
ask adults [birthday
reproduce-adults
move]
tick
end
to birthday
set age ticks
if age > 5 [set breed adults]
if age > 10 [set breed elderlies]
if age > 15 [die]
end
to reproduce-adults
set z random 100
if z > 65
[ hatch-kids 1]
end
to move
rt random 360
fd 1
end
source to share
Alan's solution is good if you want to age
equal the number of ticks since the turtle was created. You can divide this number by another number if you want a different age. For example, if ticks represent months, you can divide by 12 to get years.
Another method involves adding birth-tick
- the check mark the turtle was "born on" as a variable turtles-own
. Let's say you want to increase the age every 5 ticks. Then you can write increment-age
like this:
to increment-age
let age-in-ticks birth-tick - ticks
if (age-in-ticks > 0) and (age-in-ticks mod 5 = 0) [
set age (1 + age)
]
end
An expression containing mod
checks whether the number of ticks since birth is divisible by 5.
EDIT: I have to clarify that you need to set the birth-tick
value ticks
at the time each turtle is created.
source to share