sim <- simInit()
# these put the functions in the parent.frame() which is .GlobalEnv for an interactive user
defineEvent(sim, "init", moduleName = "thisTestModule", code = {
sim <- Init(sim) # initialize
# Now schedule some different event for "current time", i.e., will
# be put in the event queue to run *after* this current event is finished
sim <- scheduleEvent(sim, time(sim), "thisTestModule", "grow")
}, envir = envir(sim))
defineEvent(sim, "grow", moduleName = "thisTestModule", code = {
sim <- grow(sim) # grow
# Now schedule this same event for "current time plus 1", i.e., a "loop"
sim <- scheduleEvent(sim, time(sim) + 1, "thisTestModule", "grow") # for "time plus 1"
})
Init <- function(sim) {
sim$messageToWorld <- "Now the sim has an object in it that can be accessed"
sim$size <- 1 # initializes the size object --> this can be anything, Raster, list, whatever
message(sim$messageToWorld)
return(sim) # returns all the things you added to sim as they are in the simList
}
grow <- function(sim) {
sim$size <- sim$size + 1 # increments the size
message(sim$size)
return(sim)
}
# schedule that first "init" event
sim <- scheduleEvent(sim, 0, "thisTestModule", "init")
# Look at event queue
events(sim) # shows the "init" we just added
# \donttest{
# this is skipped when running in automated tests; it is fine in interactive use
out <- spades(sim)
# }
Run the code above in your browser using DataLab