[plt-scheme] MrEd and continuations

From: Henk Boom (lunarc.lists at gmail.com)
Date: Thu Jan 18 12:19:26 EST 2007

Hi, I'm trying to write a simple game engine to use with opengl and
mred. Even though such applications must run with a main
update-draw-checkevents loop, I would like to express the behaviour of
a "game unit" (basically an object with callbacks for update, draw,
handle key event. . .) as a linear sequence of steps. I have decided
to use continuations for this.

Here is a very contrived example of the kind of thing I am trying to do:

(define behavior
  (let ([exit '*])
    (letrec ([execute
               (lambda ()
                 (do ((i 0 (+ 1 i))) (#f)
                   (printf "behavior ~a~n" i)
                   (call/cc (lambda (x)
                               (set! execute x)
      (lambda ()
        (call/cc (lambda (x)
                   (set! exit x)

Of course, with a behaviour this simple it would make more sense to
simply manually store the state and increment it once a frame, but
this is for example only. A more usual behaviour would be something
which provides the player with a sequence of levels, or sets up a
menu, waits for input (done by returning right away except when it's
time to do something), then shows a new menu. This could also be
extended to providing AI for individual units on the playing field, as
their behaviours could then be implemented in a linear fashion instead
of manually managing a complicated state machine.

In any case, a problem arises when I actually try and use this code.
It seems that MrEd sets up a continuation barrier around its callbacks
(I'm curious as to the reasons for this), so the continuation I make
one update can no longer be called in the next update. What should I
do about this? Should I be looking for a different way to implement

Thanks in advance
        Henk Boom

Posted on the users mailing list.