views:

40

answers:

2

I am porting an old game from C to Javascript. I have run into an issue with display code where I would like to have the main game code call display methods without having to worry about how those status messages are displayed.

In the original code, if the message is too long, the program just waits for the player to toggle through the messages with the spacebar and then continues. This doesn't work in javascript, because while I wait for an event, all of the other program code continues. I had thought to use a callback so that further code can execute when the player hits the designated key, but I can't see how that will be viable with a lot of calls to display.update(msg) scattered throughout the code.

Can I architect things differently so the event-based, asynchronous model works, or is there some other solution that would allow me to implement a more traditional event loop?

Am I making sense?

Example:

// this is what the original code does, but obviously doesn't work in Javascript
display = {
    update : function(msg) {
          // if msg is too long
          //     wait for user input
          // ok, we've got input, continue
    }
};


// this is more javascript-y...
display = {
    update : function(msg, when_finished) {
          // show part of the message

          $(document).addEvent('keydown', function(e) {
               // display the rest of the message

               when_finished();
          });
    }
};
// but makes for amazingly nasty game code
do_something(param, function() {
    // in case do_something calls display I have to 
    // provide a callback for everything afterwards

    // this happens next, but what if do_the_next_thing needs to call display?
    // I have to wait again
    do_the_next_thing(param, function() {
        // now I have to do this again, ad infinitum
    }
}
A: 

Have a flag that you are waiting for user input.

var isWaiting = false;

and then check the value of that flag in do_something (obviously set it where necessary as well :) ).

if (isWaiting) return;

You might want to implement this higher up the call stack (what calls do_something()?), but this is the approach you need.

Matt
The problem is still the same though if I'm thinking correctly. I'd need to check for that flag all over the place and if I return I would lose where I was in the program's execution. I still need to continue program execution from where the display method is called.
Brian Ramsay
if you have work to do after showing a message, defer showing the message until the end of your processing loop. set the flag before the loop ands, and the next time your processing method fires (you're using an interval, right?) check the flag before anything else.
lincolnk
+1  A: 

The short answer is "no."

The longer answer is that, with "web workers" (part of HTML5), you may be able to do it, because it allows you to put the game logic on a separate thread, and use messaging to push keys from the user input into the game thread. However, you'd then need to use messaging the other way, too, to be able to actually display the output, which probably won't perform all that well.

Jon Watte