Struct MessageWriter
pub struct MessageWriter<'w, M>where
M: Message,{ /* private fields */ }Expand description
Writes Messages of type T.
This system parameter takes exclusive access to the Messages<T> resource,
so a system cannot also have a MessageReader parameter of the same type.
If you need to both read and write messages of the same type,
use MessageMutator.
§Usage
MessageWriters are usually declared as a [SystemParam].
#[derive(Message)]
pub struct MyMessage(String); // Custom message type.
fn my_system(mut writer: MessageWriter<MyMessage>) {
writer.write(MyMessage("My custom payload!".to_string()));
}
§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§
§impl<'w, M> MessageWriter<'w, M>where
M: Message,
impl<'w, M> MessageWriter<'w, M>where
M: Message,
pub fn write(&mut self, message: M) -> MessageId<M>
pub fn write(&mut self, message: M) -> MessageId<M>
Writes an message, which can later be read by MessageReaders.
This method returns the ID of the written message.
See Messages for details.
pub fn write_batch(
&mut self,
messages: impl IntoIterator<Item = M>,
) -> WriteBatchIds<M>
pub fn write_batch( &mut self, messages: impl IntoIterator<Item = M>, ) -> WriteBatchIds<M>
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.
pub fn write_default(&mut self) -> MessageId<M>where
M: Default,
pub fn write_default(&mut self) -> MessageId<M>where
M: Default,
Trait Implementations§
§impl<M> SystemParam for MessageWriter<'_, M>where
M: Message,
impl<M> SystemParam for MessageWriter<'_, M>where
M: Message,
§type Item<'w, 's> = MessageWriter<'w, M>
type Item<'w, 's> = MessageWriter<'w, M>
Self, instantiated with new lifetimes. Read more§fn init_state(world: &mut World) -> <MessageWriter<'_, M> as SystemParam>::State
fn init_state(world: &mut World) -> <MessageWriter<'_, M> as SystemParam>::State
State.§fn init_access(
state: &<MessageWriter<'_, M> as SystemParam>::State,
system_meta: &mut SystemMeta,
component_access_set: &mut FilteredAccessSet,
world: &mut World,
)
fn init_access( state: &<MessageWriter<'_, M> as SystemParam>::State, system_meta: &mut SystemMeta, component_access_set: &mut FilteredAccessSet, world: &mut World, )
§fn apply(
state: &mut <MessageWriter<'_, M> as SystemParam>::State,
system_meta: &SystemMeta,
world: &mut World,
)
fn apply( state: &mut <MessageWriter<'_, M> as SystemParam>::State, system_meta: &SystemMeta, world: &mut World, )
SystemParam]’s state.
This is used to apply Commands during ApplyDeferred.§fn queue(
state: &mut <MessageWriter<'_, M> as SystemParam>::State,
system_meta: &SystemMeta,
world: DeferredWorld<'_>,
)
fn queue( state: &mut <MessageWriter<'_, M> as SystemParam>::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )
ApplyDeferred.§unsafe fn get_param<'w, 's>(
state: &'s mut <MessageWriter<'_, M> as SystemParam>::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Result<<MessageWriter<'_, M> as SystemParam>::Item<'w, 's>, SystemParamValidationError>
unsafe fn get_param<'w, 's>( state: &'s mut <MessageWriter<'_, M> as SystemParam>::State, system_meta: &SystemMeta, world: UnsafeWorldCell<'w>, change_tick: Tick, ) -> Result<<MessageWriter<'_, M> as SystemParam>::Item<'w, 's>, SystemParamValidationError>
SystemParamFunction. Read more