bevy_ecs/message/
update.rs

1use crate::{
2    change_detection::Mut,
3    change_detection::Tick,
4    message::{MessageRegistry, ShouldUpdateMessages},
5    system::{Local, Res, ResMut},
6    world::World,
7};
8use bevy_ecs_macros::SystemSet;
9#[cfg(feature = "bevy_reflect")]
10use core::hash::Hash;
11
12#[doc(hidden)]
13#[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)]
14pub struct MessageUpdateSystems;
15
16/// Signals the [`message_update_system`] to run after `FixedUpdate` systems.
17///
18/// This will change the behavior of the [`MessageRegistry`] to only run after a fixed update cycle has passed.
19/// Normally, this will simply run every frame.
20pub fn signal_message_update_system(signal: Option<ResMut<MessageRegistry>>) {
21    if let Some(mut registry) = signal {
22        registry.should_update = ShouldUpdateMessages::Ready;
23    }
24}
25
26/// A system that calls [`Messages::update`](super::Messages::update) on all registered [`Messages`][super::Messages] in the world.
27pub fn message_update_system(world: &mut World, mut last_change_tick: Local<Tick>) {
28    world.try_resource_scope(|world, mut registry: Mut<MessageRegistry>| {
29        registry.run_updates(world, *last_change_tick);
30
31        registry.should_update = match registry.should_update {
32            // If we're always updating, keep doing so.
33            ShouldUpdateMessages::Always => ShouldUpdateMessages::Always,
34            // Disable the system until signal_message_update_system runs again.
35            ShouldUpdateMessages::Waiting | ShouldUpdateMessages::Ready => {
36                ShouldUpdateMessages::Waiting
37            }
38        };
39    });
40    *last_change_tick = world.change_tick();
41}
42
43/// A run condition for [`message_update_system`].
44///
45/// If [`signal_message_update_system`] has been run at least once,
46/// we will wait for it to be run again before updating the messages.
47///
48/// Otherwise, we will always update the messages.
49pub fn message_update_condition(maybe_signal: Option<Res<MessageRegistry>>) -> bool {
50    match maybe_signal {
51        Some(signal) => match signal.should_update {
52            ShouldUpdateMessages::Always | ShouldUpdateMessages::Ready => true,
53            ShouldUpdateMessages::Waiting => false,
54        },
55        None => true,
56    }
57}