rlazarus / MUSS

Multi-User Shared Setting, a modern-day reimplementation of the multiuser text game.
8 stars 1 forks source link

Implementing event hooks on db.Object #143

Open duress-to-kill opened 8 years ago

duress-to-kill commented 8 years ago

I'm interested in the instantiation of event hooks to facilitate triggered world events. Here are some thoughts on how I would approach this.

So, for example, if we wished to have a mischievous critter that did not wish to be picked up, we could define a function:

def flee(object, player, **kwargs):
    player.emit('The {} flees when {} attempts to pick it up!'
                      .format(object.name, player.name))
    object.location(get_random_adjacent_location())

(here we are assuming that get_random_adjacent_location() has been elsewhere defined and does what it says on the tin. you get the idea.)

We would then register the function to the object as an event with:

frisky_critter.register_event('get', flee)

Subsequently, when a player attempts to get the frisky_critter object, the Take.execute() method is invoked by existing code. This method would use its knowledge of the parameter list it expects to be passed to it to identify the target (if any) which is to be Taken, and call target.run_event(name[0], player, **args). This would invoke the flee() function.

I intend to implement this on my forked copy, and pending something that seems like a decent working implementation, and any comments on the issue, I'll submit a PR.