# HG changeset patch # User Simon Cross # Date 1302130022 -7200 # Node ID 023eea4ad4a53b210f138581a811ffd70706bf80 # Parent 4ac8ef4b44c365d3c0a3c40b6c8bbfd6ef7dd8d1 Add support for trigger world changes when entering and exiting dialog states. diff -r 4ac8ef4b44c3 -r 023eea4ad4a5 skaapsteker/dialogue.py --- a/skaapsteker/dialogue.py Thu Apr 07 00:38:47 2011 +0200 +++ b/skaapsteker/dialogue.py Thu Apr 07 00:47:02 2011 +0200 @@ -40,7 +40,9 @@ state = self.states[self.state] next_state = state.event(my_locals) if next_state.name in self.states: + self.states[self.state].leave(my_locals) self.state = next_state.name + self.states[self.state].enter(my_locals) def choice(self, i): self.event(DsmEvent(choice=i)) @@ -92,6 +94,18 @@ pseudo_path = base_path + ["auto_next"] self.triggers.append(Trigger("""True""", auto_next, pseudo_path)) + on_entry = state_src.get("on_entry", None) + if on_entry is not None: + self.on_entry = compile(on_entry, + "<%s>" % ":".join(base_path + ["on_entry"]), + "exec") + + on_exit = state_src.get("on_exit", None) + if on_exit is not None: + self.on_exit = compile(on_exit, + "<%s>" % ":".join(base_path + ["on_exit"]), + "exec") + def __repr__(self): return "<%r name=%r>" % (self.__class__.__name__, self.name) @@ -101,6 +115,12 @@ if next_state is not None: return next_state + def enter(self, my_locals): + self.on_entry(my_locals, {}, my_locals.copy()) + + def leave(self, my_locals): + self.on_exit(my_locals, {}, my_locals.copy()) + class Trigger(object): """Matches DSM events and triggers state transitions.