PreEvent and changes to Event
Posted: 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.
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:
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.
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 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