NetLogo doesn't remember past values, so it cannot give you the value of a variable (or result of a reporter) at a past tick. You need to save those values in your model as they are generated and this is normally done by using a list. In the code below, each turtle is setup with a time-car-history
of length 5 (specified by history-length
), that is initially filled with -1's. Then, in test
, each turtle gets of value of time-car
(here just a random number) and adds it to the beginning of its history. The current value is thus item 0
in its time-car-history
, the value of one tick before is item 1 time-car-history
, etc., going back four ticks. Note that in adding the current value to time-car-history
I drop the last value using but-last
, so only the most recent five values are saved. If you paste this code into a blank model, type "setup" at the command line and then repeatedly type "test", you should see how it works.
turtles-own [time-car-history]
to setup
clear-all
let history-length 5 ; the number of periods you want to save
create-turtles 10 [
; creates a history list of the right length, filled with -1's.
set time-car-history n-values history-length [-1]
]
reset-ticks
end
to test
ask turtles [
set time-car-history fput time-car but-last time-car-history
]
ask turtle 3 [
show time-car-history
show item 0 time-car-history
show item 1 time-car-history
show item 2 time-car-history
]
tick
end
to-report time-car
report random 10
end