PreEvent and changes to Event

Discuss API design here.
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

PreEvent and changes to Event

Postby satoon101 » Sun Aug 02, 2015 3:26 am

We just implemented a new PreEvent decorator tonight. The initial design used the name of the events, just like the Event decorator, which we then realized caused name duplication. In order to avoid this, we changed both decorators to require a string event name to be passed, and we no longer use the name of the function at all in the registration process.

Syntax: Select all

from events import Event
from events.hooks import PreEvent


@Event('player_spawn')
def my_player_spawn_function(game_event):
...


@PreEvent('player_spawn')
def my_pre_player_spawn_function(game_event):
...


PreEvent also wraps the new pre_event_manager object, just like Event with event_manager. This way, you can add/remove pre-event hooks on the fly:

Syntax: Select all

from events.hooks import pre_event_manager


def my_pre_player_spawn_function(game_event):
...

pre_event_manager.register_for_event('player_spawn', my_pre_player_spawn_function)

def unload():
pre_event_manager.unregister_for_event('player_spawn', my_pre_player_spawn_function)


With pre-events, you can either return nothing (or None) or use the new EventAction enumerator class to return the type of action to take.

Syntax: Select all

class EventAction(IntEnum):

"""Enum class used to know what to do with a pre-hooked event."""

CONTINUE = 0
STOP_BROADCAST = 1
BLOCK = 2


Note that if EventAction.BLOCK is returned by any hook for a given event, all hooks will still be ran, but the event will be blocked at the end of the iteration. And the same with STOP_BROADCAST. If any hook returns that value, and no hook returns BLOCK, the event will be fired as normal, but will not be broadcast to clients.

Syntax: Select all

from events.hooks import EventAction
from events.hooks import PreEvent

@PreEvent('player_team')
def pre_player_team(game_event):
# Stop the event from broadcasting
# For this event, this will stop the chat messages about players joining teams
return EventAction.STOP_BROADCAST

@PreEvent('player_spawn')
def pre_player_spawn(game_event):
# This will stop the event from being executed
# This means all normal event calls for player_spawn will not be called
return EventAction.BLOCK
Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Sun Aug 02, 2015 12:50 pm

Awesome! I've personally (for no reason, just a personal preference) always preferred passing the name as a string to decorators, instead of having to rely on function names.

Does the EventActions.BLOCK mean we can do a 33% evasion like so:

Syntax: Select all

from events.hooks import EventAction
from events.hooks import PreEvent
import random

@PreEvent('player_hurt')
def evasion(game_event):
if random.randint(0, 100) <= 33:
return EventAction.BLOCK
And player_hurt event will never be fired? If so, this is awesome news for my Hero-Wars project!
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Aug 02, 2015 1:19 pm

Yes, it does. :)
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Aug 02, 2015 1:37 pm

That will only block the event itself, not the actual damage to the player. To block the damage itself, pre-hook on_take_damage and return False to block that function.
Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Sun Aug 02, 2015 8:07 pm

satoon101 wrote:That will only block the event itself, not the actual damage to the player. To block the damage itself, pre-hook on_take_damage and return False to block that function.
Oh, right! So if I want to stop all skills in my Hero-Wars, as well as prevent the damage from being taken, I just pre-hook them both? Or is False from on_take_damage enough to prevent player_hurt too?
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Sun Aug 02, 2015 10:20 pm

Great job!
For those who want to know: You can now easily stop the kill feed from showing when returning EventAction.STOP_BROADCAST in player_death pre hook (no more extra hook needed for that).
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Aug 03, 2015 12:42 am

Mahi wrote:
satoon101 wrote:That will only block the event itself, not the actual damage to the player. To block the damage itself, pre-hook on_take_damage and return False to block that function.
Oh, right! So if I want to stop all skills in my Hero-Wars, as well as prevent the damage from being taken, I just pre-hook them both? Or is False from on_take_damage enough to prevent player_hurt too?


Simply hooking on_take_damage takes care of the event, too.
Image

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 7 guests