pub trait Handler: 'static {
// Required methods
fn type_id(&self) -> Option<TypeId>;
fn name(&self) -> Cow<'static, str>;
fn init(
&mut self,
world: &mut World,
config: &mut HandlerConfig,
) -> Result<(), InitError>;
unsafe fn run(
&mut self,
info: &HandlerInfo,
event_ptr: EventPtr<'_>,
target_location: EntityLocation,
world: UnsafeWorldCell<'_>,
);
fn refresh_archetype(&mut self, arch: &Archetype);
fn remove_archetype(&mut self, arch: &Archetype);
}
Expand description
A callback function that listens for events.
handlers are added to a world using the World::add_handler
method.
For more information about handlers, see the tutorial.
Required Methods§
Sourcefn init(
&mut self,
world: &mut World,
config: &mut HandlerConfig,
) -> Result<(), InitError>
fn init( &mut self, world: &mut World, config: &mut HandlerConfig, ) -> Result<(), InitError>
Initializes the handler. Returns InitError
on failure.
Sourceunsafe fn run(
&mut self,
info: &HandlerInfo,
event_ptr: EventPtr<'_>,
target_location: EntityLocation,
world: UnsafeWorldCell<'_>,
)
unsafe fn run( &mut self, info: &HandlerInfo, event_ptr: EventPtr<'_>, target_location: EntityLocation, world: UnsafeWorldCell<'_>, )
Execute the handler by passing in the handler’s metadata, a pointer to
the received event of the configured type, the entity location of
the event’s target, and an UnsafeWorldCell
with permission to
access the data described in the configuration.
§Safety
- handler must be initialized via
init
. info
must be the correct information for this handler.event_ptr
must point to the correct type of event configured by this handler ininit
.target_location
must be a valid location to an entity matching the component access set byset_targeted_event_component_access
, unless the event is not targeted.world
must have permission to access all data configured by this handler ininit
.
Sourcefn refresh_archetype(&mut self, arch: &Archetype)
fn refresh_archetype(&mut self, arch: &Archetype)
Notifies the handler that an archetype it might care about had its internal state updated.
This is invoked in the following scenarios:
- The archetype was previously empty, but has now gained at least one entity.
- The archetype’s columns were reallocated, and any pointers into the archetype’s columns need to be reacquired.
This method must not be called with empty archetypes.
Sourcefn remove_archetype(&mut self, arch: &Archetype)
fn remove_archetype(&mut self, arch: &Archetype)
Notifies the handler that an archetype it might care about is no longer available.
This is invoked in the following scenarios:
- The archetype was removed from the world.
- The archetype previously had entities in it, but is now empty.
In either case, the handler must assume that the archetype is no longer available. Attempting to read the component data from a removed archetype is illegal.