Event Handling

Event

class CommonEvent

Bases: object

A custom event, for use with the CommonEventHandler.

event_name

The name of this event.

Returns:The name of the event.
Return type:str

Event Handler

class CommonEventHandler(mod_identifier, event_function)

Bases: object

Handles events.

Parameters:
  • mod_identifier (Union[str, CommonModIdentity]) – The name or identity of the mod handling events.
  • event_function (Callable[[CommonEvent], Any]) – The function this handler invokes.
Raises:
  • RuntimeError – When event_function is None.
  • TypeError – When event_function is not a callable function.
  • AssertionError – When the event_function is missing the event_data argument, when the event_function contains a self or cls argument, or when more than one argument is found.
can_handle_event(event)

Determine if this event handler can handle the type of the event.

Parameters:event (CommonEvent) – The event to check.
Returns:True, if this handler can handle the event. False, if not.
Return type:bool
event_function

The action to take upon receiving an event.

Returns:The function that handles events.
Return type:Callable[[CommonEvent], Any]
event_type

The class type of events this Event Handler will handle.

Returns:The types of events the handler handles.
Return type:type
handle_event(event)

Handle the event data.

Parameters:event (CommonEvent) – The event to handle.
Returns:True, if the event was successful. False, if not.
Return type:bool
mod_name

The name of the mod this event handler was created for.

Returns:The name of the Mod that owns this handler.
Return type:str

Event Registry

class CommonEventRegistry

Bases: sims4communitylib.services.common_service.CommonService

Register event listeners and dispatch events to any that are registered.

dispatch(event)

Dispatch an event to any event handlers listening for it.

Note

If any listeners return False or None when they handle it, the total result of dispatch will be False as well.

Parameters:event (CommonEvent) – An instance of an Event to dispatch to listeners.
Returns:True, if the Event was dispatched to all listeners successfully. False, if any listeners failed to handle the event.
Return type:bool
static handle_events(mod_identifier)

Decorate functions with this static method to register that function to handle an event.

:warning:: Event functions MUST be decorated with staticmethod and must only have a single argument with the name ‘event_data’ (Errors will be thrown upon loading the game otherwise)

Parameters:mod_identifier (Union[str, CommonModIdentity]) – The name or identity of the mod the class is being registered for.
Returns:A callable function wrapped to handle events.
Return type:Callable[[Callable[[CommonEvent], bool]], Callable[[CommonEvent], bool]]