[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Christian Reiniger wrote:
> On Wednesday 2002-04-17 19:06, Steve Baker wrote:
> > Ingo Ruhnke wrote:
> > > Isn't it much easier in such situations to do it in an event driven
> > > way:
> > >
> > > light_switch = make_light_switch(...)
> > >
> > > # Define a function that should be started when someone hits the
> > > light # switch
> > > def on_lightswitch_press ():
> > > toggle ( switch_graphic )
> > > toggle ( lightbulb_graphic )
> > > ...
> > >
> > > # Register the function for the on_press event of the light_switch
> > > light_switch.on_press = on_lightswitch_press
> > Well, that doesn't reproduce what my switch does - which is to make
> > it impossible to turn the light switch off for ten seconds after you
> > turned it on...but in any case, this was just a silly example - real
> > scripts get much more complex.
> def on_lightswitch_press ():
> if (last_pressed < (now - 10 sec))
> toggle (switch_graphic)
> toggle (lighbulb_graphic)
> last_pressed = now
I'm not saying you can't do it - to the contrary, it's been proven that
you can ALWAYS turn a program inside out and turn it into this kind of
thing - I'm just saying that for anything remotely complicated, this
approach gets untenable fast.
> > You *can* turn any simple program into a state machine and vice-versa
> > (you can prove that with math) - but it gets increasingly hard to get
> > your head around the resulting code as you have more ongoing activities
> > for your game object.
> Note that Ingo is *not* talking about a state machine but about
> event-driven code!
Well, event-driven code that *isn't* state-machine code can't work for all
You end up having to turn event handlers on and off depending on some
additional constraints - and typically turning them on and off INSIDE
the event handlers. That rapidly turns into a state machine.
In effect, a simple event-based mechanism is just a state machine with
only one state...(Imagine a state machine diagram where there is one
state - and each 'event' transitions you through some action and back
into that one state...that's *identical* to an event mechanism where you
can't enable and disable events).
Once you start turning event handlers on and off inside the handlers,
you have (in effect) added multiple 'states' where the current state is
just the current set of event handlers. It's kinda hard to explain - but
trust me - I did this for a living once. [NEVER get a job programming
telephone switches - it's BORING, BORING, DULL and BORING..and if they
want you to do it in SDL...RUN AWAY SCREAMING!]
----------------------------- Steve Baker -------------------------------
Mail : <firstname.lastname@example.org> WorkMail: <email@example.com>
URLs : http://www.sjbaker.org
http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net