pub struct MessageWriter<'w, E: Message> { /* private fields */ }Expand description
Writes Messages of type T.
§Usage
MessageWriters are usually declared as a SystemParam.
#[derive(Message)]
pub struct MyMessage; // Custom message type.
fn my_system(mut writer: MessageWriter<MyMessage>) {
writer.write(MyMessage);
}
§Concurrency
MessageWriter param has ResMut<Messages<T>> inside. So two systems declaring MessageWriter<T> params
for the same message type won’t be executed concurrently.
§Untyped messages
MessageWriter can only write messages of one specific type, which must be known at compile-time.
This is not a problem most of the time, but you may find a situation where you cannot know
ahead of time every kind of message you’ll need to write. In this case, you can use the “type-erased message” pattern.
fn write_untyped(mut commands: Commands) {
// Write a message of a specific type without having to declare that
// type as a SystemParam.
//
// Effectively, we're just moving the type parameter from the /type/ to the /method/,
// which allows one to do all kinds of clever things with type erasure, such as sending
// custom messages to unknown 3rd party plugins (modding API).
//
// NOTE: the message won't actually be sent until commands get applied during
// apply_deferred.
commands.queue(|w: &mut World| {
w.write_message(MyMessage);
});
}Note that this is considered non-idiomatic, and should only be used when MessageWriter will not work.
Implementations§
Source§impl<'w, E: Message> MessageWriter<'w, E>
impl<'w, E: Message> MessageWriter<'w, E>
Sourcepub fn write(&mut self, message: E) -> MessageId<E>
pub fn write(&mut self, message: E) -> MessageId<E>
Writes an message, which can later be read by MessageReaders.
This method returns the ID of the written message.
See Messages for details.
Sourcepub fn write_batch(
&mut self,
messages: impl IntoIterator<Item = E>,
) -> WriteBatchIds<E> ⓘ
pub fn write_batch( &mut self, messages: impl IntoIterator<Item = E>, ) -> WriteBatchIds<E> ⓘ
Writes a list of messages all at once, which can later be read by MessageReaders.
This is more efficient than writing each message individually.
This method returns the IDs of the written messages.
See Messages for details.
Trait Implementations§
Source§impl<E: Message> SystemParam for MessageWriter<'_, E>
impl<E: Message> SystemParam for MessageWriter<'_, E>
Source§type Item<'w, 's> = MessageWriter<'w, E>
type Item<'w, 's> = MessageWriter<'w, E>
Self, instantiated with new lifetimes. Read moreSource§fn init_access(
state: &Self::State,
system_meta: &mut SystemMeta,
component_access_set: &mut FilteredAccessSet,
world: &mut World,
)
fn init_access( state: &Self::State, system_meta: &mut SystemMeta, component_access_set: &mut FilteredAccessSet, world: &mut World, )
World access used by this SystemParamSource§fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
SystemParam’s state.
This is used to apply Commands during ApplyDeferred.Source§fn queue(
state: &mut Self::State,
system_meta: &SystemMeta,
world: DeferredWorld<'_>,
)
fn queue( state: &mut Self::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )
ApplyDeferred.Source§unsafe fn validate_param<'w, 's>(
state: &'s mut Self::State,
_system_meta: &SystemMeta,
_world: UnsafeWorldCell<'w>,
) -> Result<(), SystemParamValidationError>
unsafe fn validate_param<'w, 's>( state: &'s mut Self::State, _system_meta: &SystemMeta, _world: UnsafeWorldCell<'w>, ) -> Result<(), SystemParamValidationError>
Source§unsafe fn get_param<'w, 's>(
state: &'s mut Self::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Self::Item<'w, 's>
unsafe fn get_param<'w, 's>( state: &'s mut Self::State, system_meta: &SystemMeta, world: UnsafeWorldCell<'w>, change_tick: Tick, ) -> Self::Item<'w, 's>
SystemParamFunction. Read more