I'm interested in the instantiation of event hooks to facilitate triggered world events. Here are some thoughts on how I would approach this.
Add a generic event hook method to the base Object class, which is to be invoked explicitly in the execute method of each class derived from parser.Command
The method signature I propose is: run_event(self, event_name, player, **kwargs)
Add a generic event registration method to the base Object class, by means of which a script can be attached to an object with a corresponding event name which causes that script to run.
The method signature I propose is: register_event(self, event_name, event)
event is expected to be a function, which can be called with the following signature: event(object, player, **kwargs)
the object parameter shall be a reference to the object on which the event is registered.
event_name shall be a comparable, nonempty value. When run_event() is called with a matching value, event() shall be called immediately.
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.
I'm interested in the instantiation of event hooks to facilitate triggered world events. Here are some thoughts on how I would approach this.
run_event(self, event_name, player, **kwargs)
register_event(self, event_name, event)
event
is expected to be a function, which can be called with the following signature:event(object, player, **kwargs)
object
parameter shall be a reference to the object on which the event is registered.event_name
shall be a comparable, nonempty value. Whenrun_event()
is called with a matching value,event()
shall be called immediately.So, for example, if we wished to have a mischievous critter that did not wish to be picked up, we could define a function:
(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:
Subsequently, when a player attempts to get the
frisky_critter
object, theTake.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 beTake
n, and calltarget.run_event(name[0], player, **args)
. This would invoke theflee()
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.