2020-10-15 22:49:05

Hi all,
I know it's only been a day since I posted, but this has been a plan on my mind for a while, and the new Earwax promises framework gave me the ability to bring MOO-style suspends into Python.

For those of you who don't know, when you're coding using the MOO programming language, you have 2 ways of temporarily suspending a function:

player.tell("Hello there.");
suspend(2);
player.tell("That took a while.");
fork(5)
  player.tell("That took even longer.");
endfork

It's been a while since I coded in MOO, but you get the idea.

With Python's generators, you can kind of simulate that behaviour, which is what the new StaggeredPromise class lets you do with earwax:

from earwax import StaggeredPromise
from earwax.types import StaggeredPromiseGeneratorType


@StaggeredPromise.decorate
def promise() -> StaggeredPromiseGeneratorType:
    game.output('Hello world.')
    yield 2.0
    game.output('That took a while.')
    yield 5.0
    game.output('That took even longer.')


promise.run()

As with ThreadedPromise, you can catch the return value, and any raised errors. You can also get notified every time a promise suspends:

@promise.event
def on_next(delay: float) -> None:
    game.interface_player.play_sound(Path('sound.wav'))

I hope you enjoy using the StaggeredPromise class for splitting up your games a little, without messing about with any of Pyglet's schedule functions.

Also, sorry for the silly name... It's the best of the bad bunch I could come up with.

Take care,
Chris Norman
Selling my soul to andertons.co.uk since 2012.