[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scripting



Steve Baker <sjbaker1@airmail.net> writes:

> I'd like things like:
> 
>   behaviour_for_light_switch ()
>   {
>     while ( 1 )
>     {
>       sleep ( 10 frames ) ;
> 
>       if ( someone_hit_the_light_switch )
>       {
>         toggle ( switch_graphic ) ;
>         toggle ( lightbulb_graphic ) ;
>         if ( lightbulb_graphic == ON )
>         {
>           sleep ( 10 seconds ) ;
>           signal ( security_system, INTRUSION_DETECTED ) ;
>         }
>       }
>     }
>   }
> 
> ..and run one copy of this (or a slight variation of it) for every
> light switch in a 100 room building, another for every creature, another
> for every collectable...you name it, it has a short script for it.

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

world.add(light_switch)

Thats basically what I do in Advent (see sig.) and its working out
pretty well. I have also events which gets triggered when something is
done, like when a person has reached its target. The access to the
event is actually the return value of the function call that triggered
the walk, so I can do things like:

 hook = some_person.walk_to(house)
 
 def do_something_when_house_is_reached():
        ...
        ...

 # Call do_something_when_house_is_reached when some_person reached
 # the house

 hook.register (do_something_when_house_is_reached)

With some macro-magic (basically hiding the hook registration for the
event) I can do even things like:

sequenze:
 person1.walk_to(house)
 person2.walk_to(person1)
 person1.talk_to(person2, "blabla...")

Which is pretty handy for cut-scenes and background activity. Since
the script code is only called at specific events, it never has to run
in its own thread or something like that.

-- 
| Ingo Ruhnke <grumbel@gmx.de> | Advent:            http://advent.sf.net/ |
|==============================| Pingus:          http://pingus.seul.org/ |
| JabberID: grumbel@jabber.org | Feuerkraft:    http://feuerkraft.sf.net/ |
| ICQ: 59461927                | Home:   http://pingus.seul.org/~grumbel/ |