Undum / Raconteur realtime events

I need to add a sound to a Raconteur game, a sound of steps. The idea is to play an audio file every 1 second.

But! I have several files and I would like to randomize between them. And also the volume should depend on the player actions.

So I need some kind of link between Undum and setInterval function, some variable to save the current volume. And I would prefer to keep this link when saving/reloading the page.

The problem is, as I’m sure you know, that Undum is a Javascript closure. You can’t just access the Undum character object from the setInterval call, so the link should be a strict one-way from the game to the DOM.

I know lots of messy ways to do it and no clean one. So, is there one?

I would look into Vorple which I understand has Undum-friendly bindings, though I am personally unfamiliar with it.

Otherwise, I would build your library for playing sounds, then make a little API for it and call it from inside situations to set how you would like sound to play. Undum saving/loading works by recapitulation; ie, when you load an Undum game with a saved state, what it does is it just replays the sequence of links that was saved previously (this is why System is used to ensure the same random seed is used each time). So my initial thought would be to have a “heartbeat” that plays ambient sounds every N seconds, and have situations use that API to set the state of the sound-playing object; this should work with saving and loading fine (I mean, it the first heartbeat happens too quickly, it could create a race condition on very slow machines that take a while to load and run through the entire save, but I don’t think this is a realistic problem).

It is a realistic problem because slow GPRS internet and slow smartphones are still very popular.

You can access the character object as long as it’s in scope when you define the interval. undum.game.init is a good place to do that.

undum.game.init = function(character, system) {
  setInterval( function() {
    console.log( 'Character object:', character );
  }, 1000 );