Skip to content

Commit

Permalink
println debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Jun 11, 2024
1 parent c813e66 commit 806b615
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
28 changes: 15 additions & 13 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ mod tests {
change_detection::{DetectChanges, ResMut},
component::Component,
entity::Entity,
event::{Event, EventWriter, Events},
event::{Event, EventWriter},
query::With,
removal_detection::RemovedComponents,
schedule::{IntoSystemConfigs, ScheduleLabel},
Expand Down Expand Up @@ -1280,35 +1280,37 @@ mod tests {
#[derive(Event, Clone)]
struct TestEvent;

println!("Starting test");

let mut app = App::new();
app.add_event::<TestEvent>();

// Starts empty
let test_events = app.world().resource::<Events<TestEvent>>();
assert_eq!(test_events.len(), 0);
assert_eq!(test_events.iter_current_update_events().count(), 0);
//let test_events = app.world().resource::<Events<TestEvent>>();
//assert_eq!(test_events.len(), 0);
//assert_eq!(test_events.iter_current_update_events().count(), 0);
app.update();

// Sending one event
app.world_mut().send_event(TestEvent);

let test_events = app.world().resource::<Events<TestEvent>>();
assert_eq!(test_events.len(), 1);
assert_eq!(test_events.iter_current_update_events().count(), 1);
//let test_events = app.world().resource::<Events<TestEvent>>();
//assert_eq!(test_events.len(), 1);
//assert_eq!(test_events.iter_current_update_events().count(), 1);
app.update();

// Sending two events on the next frame
app.world_mut().send_event(TestEvent);
app.world_mut().send_event(TestEvent);

let test_events = app.world().resource::<Events<TestEvent>>();
assert_eq!(test_events.len(), 3); // Events are double-buffered, so we see 1 + 2 = 3
assert_eq!(test_events.iter_current_update_events().count(), 2);
//let test_events = app.world().resource::<Events<TestEvent>>();
//assert_eq!(test_events.len(), 3); // Events are double-buffered, so we see 1 + 2 = 3
//assert_eq!(test_events.iter_current_update_events().count(), 2);
app.update();

// Sending zero events
let test_events = app.world().resource::<Events<TestEvent>>();
assert_eq!(test_events.len(), 2); // Events are double-buffered, so we see 2 + 0 = 2
assert_eq!(test_events.iter_current_update_events().count(), 0);
//let test_events = app.world().resource::<Events<TestEvent>>();
//assert_eq!(test_events.len(), 2); // Events are double-buffered, so we see 2 + 0 = 2
//assert_eq!(test_events.iter_current_update_events().count(), 0);
}
}
1 change: 1 addition & 0 deletions crates/bevy_app/src/sub_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ impl SubApp {
{
if !self.world.contains_resource::<Events<T>>() {
EventRegistry::register_event::<T>(self.world_mut());
println!("Registered event: {:?}", std::any::type_name::<T>());
}

self
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/event/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ impl<E: Event> Events<E> {
///
/// If you need access to the events that were removed, consider using [`Events::update_drain`].
pub fn update(&mut self) {
println!("Updating events!");

std::mem::swap(&mut self.events_a, &mut self.events_b);
self.events_b.clear();
self.events_b.start_event_count = self.event_count;
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_ecs/src/event/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bevy_ecs::{
};

#[doc(hidden)]
#[derive(Debug)]
struct RegisteredEvent {
component_id: ComponentId,
// Required to flush the secondary buffer and drop events even if left unchanged.
Expand All @@ -19,7 +20,7 @@ struct RegisteredEvent {

/// A registry of all of the [`Events`] in the [`World`], used by [`event_update_system`]
/// to update all of the events.
#[derive(Resource, Default)]
#[derive(Resource, Default, Debug)]
pub struct EventRegistry {
pub(super) needs_update: bool,
event_updates: Vec<RegisteredEvent>,
Expand All @@ -46,7 +47,11 @@ impl EventRegistry {

/// Updates all of the registered events in the World.
pub fn run_updates(&mut self, world: &mut World, last_change_tick: Tick) {
println!("Running event updates");

for registered_event in &mut self.event_updates {
println!("Checking if we should update an event");

// Bypass the type ID -> Component ID lookup with the cached component ID.
if let Some(events) = world.get_resource_mut_by_id(registered_event.component_id) {
let has_changed = events.has_changed_since(last_change_tick);
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_ecs/src/event/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn signal_event_update_system(signal: Option<ResMut<EventRegistry>>) {

/// A system that calls [`Events::update`] on all registered [`Events`] in the world.
pub fn event_update_system(world: &mut World, mut last_change_tick: Local<Tick>) {
println!("Running event_update_system");
if world.contains_resource::<EventRegistry>() {
world.resource_scope(|world, mut registry: Mut<EventRegistry>| {
registry.run_updates(world, *last_change_tick);
Expand All @@ -35,6 +36,10 @@ pub fn event_update_system(world: &mut World, mut last_change_tick: Local<Tick>)

/// A run condition for [`event_update_system`].
pub fn event_update_condition(signal: Option<Res<EventRegistry>>) -> bool {
println!("Checking if we should run event_update_system");

println!("signal: {:?}", signal);

// If we haven't got a signal to update the events, but we *could* get such a signal
// return early and update the events later.
signal.map_or(false, |signal| signal.needs_update)
Expand Down

0 comments on commit 806b615

Please sign in to comment.