Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing event hooks on db.Object #143

Open
duress-to-kill opened this issue Jun 15, 2016 · 0 comments
Open

Implementing event hooks on db.Object #143

duress-to-kill opened this issue Jun 15, 2016 · 0 comments

Comments

@duress-to-kill
Copy link

duress-to-kill commented Jun 15, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant