Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/src/event/update.rs
6600 views
1
use bevy_ecs::{
2
change_detection::Mut,
3
component::Tick,
4
event::EventRegistry,
5
system::{Local, Res, ResMut},
6
world::World,
7
};
8
use bevy_ecs_macros::SystemSet;
9
#[cfg(feature = "bevy_reflect")]
10
use core::hash::Hash;
11
12
use super::registry::ShouldUpdateEvents;
13
14
#[doc(hidden)]
15
#[derive(SystemSet, Clone, Debug, PartialEq, Eq, Hash)]
16
pub struct EventUpdateSystems;
17
18
/// Deprecated alias for [`EventUpdateSystems`].
19
#[deprecated(since = "0.17.0", note = "Renamed to `EventUpdateSystems`.")]
20
pub type EventUpdates = EventUpdateSystems;
21
22
/// Signals the [`event_update_system`] to run after `FixedUpdate` systems.
23
///
24
/// This will change the behavior of the [`EventRegistry`] to only run after a fixed update cycle has passed.
25
/// Normally, this will simply run every frame.
26
pub fn signal_event_update_system(signal: Option<ResMut<EventRegistry>>) {
27
if let Some(mut registry) = signal {
28
registry.should_update = ShouldUpdateEvents::Ready;
29
}
30
}
31
32
/// A system that calls [`Events::update`](super::Events::update) on all registered [`Events`][super::Events] in the world.
33
pub fn event_update_system(world: &mut World, mut last_change_tick: Local<Tick>) {
34
world.try_resource_scope(|world, mut registry: Mut<EventRegistry>| {
35
registry.run_updates(world, *last_change_tick);
36
37
registry.should_update = match registry.should_update {
38
// If we're always updating, keep doing so.
39
ShouldUpdateEvents::Always => ShouldUpdateEvents::Always,
40
// Disable the system until signal_event_update_system runs again.
41
ShouldUpdateEvents::Waiting | ShouldUpdateEvents::Ready => ShouldUpdateEvents::Waiting,
42
};
43
});
44
*last_change_tick = world.change_tick();
45
}
46
47
/// A run condition for [`event_update_system`].
48
///
49
/// If [`signal_event_update_system`] has been run at least once,
50
/// we will wait for it to be run again before updating the events.
51
///
52
/// Otherwise, we will always update the events.
53
pub fn event_update_condition(maybe_signal: Option<Res<EventRegistry>>) -> bool {
54
match maybe_signal {
55
Some(signal) => match signal.should_update {
56
ShouldUpdateEvents::Always | ShouldUpdateEvents::Ready => true,
57
ShouldUpdateEvents::Waiting => false,
58
},
59
None => true,
60
}
61
}
62
63