deregister events (#14083)
# Objective Add ability to de-register events from the EventRegistry (and the associated World). The initial reasoning relates to retaining support for Event hot reloading in `dexterous_developer`. ## Solution Add a `deregister_events<T: Event>(&mut world)` method to the `EventRegistry` struct. ## Testing Added an automated test that verifies the event registry adds and removes `Events<T>` from the world. --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									856b39d821
								
							
						
					
					
						commit
						bd7dcd3f6d
					
				| @ -280,6 +280,24 @@ mod tests { | ||||
|         assert!(is_empty, "EventReader should be empty"); | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_event_registry_can_add_and_remove_events_to_world() { | ||||
|         use bevy_ecs::prelude::*; | ||||
| 
 | ||||
|         let mut world = World::new(); | ||||
|         EventRegistry::register_event::<TestEvent>(&mut world); | ||||
| 
 | ||||
|         let has_events = world.get_resource::<Events<TestEvent>>().is_some(); | ||||
| 
 | ||||
|         assert!(has_events, "Should have the events resource"); | ||||
| 
 | ||||
|         EventRegistry::deregister_events::<TestEvent>(&mut world); | ||||
| 
 | ||||
|         let has_events = world.get_resource::<Events<TestEvent>>().is_some(); | ||||
| 
 | ||||
|         assert!(!has_events, "Should not have the events resource"); | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_update_drain() { | ||||
|         let mut events = Events::<TestEvent>::default(); | ||||
|  | ||||
| @ -41,7 +41,10 @@ pub enum ShouldUpdateEvents { | ||||
| } | ||||
| 
 | ||||
| impl EventRegistry { | ||||
|     /// Registers an event type to be updated.
 | ||||
|     /// Registers an event type to be updated in a given [`World`]
 | ||||
|     ///
 | ||||
|     /// If no instance of the [`EventRegistry`] exists in the world, this will add one - otherwise it will use
 | ||||
|     /// the existing instance.
 | ||||
|     pub fn register_event<T: Event>(world: &mut World) { | ||||
|         // By initializing the resource here, we can be sure that it is present,
 | ||||
|         // and receive the correct, up-to-date `ComponentId` even if it was previously removed.
 | ||||
| @ -77,4 +80,14 @@ impl EventRegistry { | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// Removes an event from the world and it's associated [`EventRegistry`].
 | ||||
|     pub fn deregister_events<T: Event>(world: &mut World) { | ||||
|         let component_id = world.init_resource::<Events<T>>(); | ||||
|         let mut registry = world.get_resource_or_insert_with(Self::default); | ||||
|         registry | ||||
|             .event_updates | ||||
|             .retain(|e| e.component_id != component_id); | ||||
|         world.remove_resource::<Events<T>>(); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Lee-Orr
						Lee-Orr