Next: , Up: Scripting   [Contents][Index]


2.5.1 Agendas

To schedule a task to be performed later, an “agenda” is used. There is a default, global agenda that is ready to be used, or additional agendas may be created for different purposes. The following example prints the text “hello” when the agenda has advanced to time unit 10.

(at 10 (display "hello\n"))

Most of the time it is more convenient to schedule tasks relative to the current time. This is where after comes in handy:

(after 10 (display "hello\n"))

Time units in the agenda are in no way connected to real time. It’s up to the programmer to decide what agenda time means. A simple and effective approach is to map each call of the update procedure (see Kernel) to 1 unit of agenda time, like so:

(define (update dt)
  (update-agenda 1))

It is important to call update-agenda periodically, otherwise no tasks will ever be run!

In addition to using the global agenda, it is useful to have multiple agendas for different purposes. For example, the game world can use a different agenda than the user interface, so that pausing the game is a simple matter of not updating the world’s agenda while continuing to update the user interface’s agenda. The current agenda is dynamically scoped and can be changed using the with-agenda special form:

(define game-world-agenda (make-agenda))

(with-agenda game-world-agenda
  (at 60 (spawn-goblin))
  (at 120 (spawn-goblin))
  (at 240 (spawn-goblin-king)))
Procedure: make-agenda

Return a new task scheduler.

Procedure: agenda? obj

Return #t if obj is an agenda.

Procedure: current-agenda
Procedure: current-agenda agenda

When called with no arguments, return the current agenda. When called with one argument, set the current agenda to agenda.

Syntax: with-agenda agenda body …

Evaluate body with the current agenda set to agenda.

Procedure: agenda-time

Return the current agenda time.

Procedure: update-agenda dt

Advance the current agenda by dt.

Procedure: schedule-at time thunk

Schedule thunk, a procedure of zero arguments, to be run at time.

Procedure: schedule-after delay thunk

Schedule thunk, a procedure of zero arguments, to be run after delay.

Procedure: schedule-every interval thunk [n]

Schedule thunk, a procedure of zero arguments, to be run every interval amount of time. Repeat this n times, or indefinitely if not specified.

Syntax: at time body …

Schedule body to be evaluated at time.

Syntax: after delay body …

Schedule body to be evaluated after delay.

Syntax: every interval body …
Syntax: every (interval n) body …

Schedule body to be evaluated every interval amount of time. Repeat this n times, or indefinitely if not specified.

It is also possible to schedule things that are not dependent on how much time passes. The agenda will periodically poll to see if any registered conditions are met.

Procedure: call-when pred thunk

Call thunk sometime in the future when pred is satisfied (returns a value other than #f.)


Next: , Up: Scripting   [Contents][Index]