Skip to content

Commit a8c610a

Browse files
Add unregister_system command (#16340)
# Objective Fixes #16266 ## Solution Added an `UnregisterSystem` command struct and `Commands::unregister_system`. Also renamed `World::remove_system` and `World::remove_system_cached` to `World::unregister_*` ## Testing It's a fairly simple change, but I tested locally to ensure it actually works. --------- Co-authored-by: Benjamin Brienen <benjamin.brienen@outlook.com>
1 parent 4225848 commit a8c610a

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::{marker::PhantomData, panic::Location};
44

55
use super::{
66
Deferred, IntoObserverSystem, IntoSystem, RegisterSystem, Resource, RunSystemCachedWith,
7+
UnregisterSystem,
78
};
89
use crate::{
910
self as bevy_ecs,
@@ -890,6 +891,17 @@ impl<'w, 's> Commands<'w, 's> {
890891
SystemId::from_entity(entity)
891892
}
892893

894+
/// Removes a system previously registered with [`Commands::register_system`] or [`World::register_system`].
895+
///
896+
/// See [`World::unregister_system`] for more information.
897+
pub fn unregister_system<I, O>(&mut self, system_id: SystemId<I, O>)
898+
where
899+
I: SystemInput + Send + 'static,
900+
O: Send + 'static,
901+
{
902+
self.queue(UnregisterSystem::new(system_id));
903+
}
904+
893905
/// Similar to [`Self::run_system`], but caching the [`SystemId`] in a
894906
/// [`CachedSystemId`](crate::system::CachedSystemId) resource.
895907
///

crates/bevy_ecs/src/system/system_registry.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct SystemIdMarker;
3030
/// A system that has been removed from the registry.
3131
/// It contains the system and whether or not it has been initialized.
3232
///
33-
/// This struct is returned by [`World::remove_system`].
33+
/// This struct is returned by [`World::unregister_system`].
3434
pub struct RemovedSystem<I = (), O = ()> {
3535
initialized: bool,
3636
system: BoxedSystem<I, O>,
@@ -172,7 +172,7 @@ impl World {
172172
///
173173
/// If no system corresponds to the given [`SystemId`], this method returns an error.
174174
/// Systems are also not allowed to remove themselves, this returns an error too.
175-
pub fn remove_system<I, O>(
175+
pub fn unregister_system<I, O>(
176176
&mut self,
177177
id: SystemId<I, O>,
178178
) -> Result<RemovedSystem<I, O>, RegisteredSystemError<I, O>>
@@ -412,7 +412,7 @@ impl World {
412412
/// Removes a cached system and its [`CachedSystemId`] resource.
413413
///
414414
/// See [`World::register_system_cached`] for more information.
415-
pub fn remove_system_cached<I, O, M, S>(
415+
pub fn unregister_system_cached<I, O, M, S>(
416416
&mut self,
417417
_system: S,
418418
) -> Result<RemovedSystem<I, O>, RegisteredSystemError<I, O>>
@@ -424,7 +424,7 @@ impl World {
424424
let id = self
425425
.remove_resource::<CachedSystemId<S::System>>()
426426
.ok_or(RegisteredSystemError::SystemNotCached)?;
427-
self.remove_system(id.0)
427+
self.unregister_system(id.0)
428428
}
429429

430430
/// Runs a cached system, registering it if necessary.
@@ -544,6 +544,32 @@ where
544544
}
545545
}
546546

547+
/// The [`Command`] type for unregistering one-shot systems from [`Commands`](crate::system::Commands).
548+
pub struct UnregisterSystem<I: SystemInput + 'static, O: 'static> {
549+
system_id: SystemId<I, O>,
550+
}
551+
552+
impl<I, O> UnregisterSystem<I, O>
553+
where
554+
I: SystemInput + 'static,
555+
O: 'static,
556+
{
557+
/// Creates a new [`Command`] struct, which can be added to [`Commands`](crate::system::Commands).
558+
pub fn new(system_id: SystemId<I, O>) -> Self {
559+
Self { system_id }
560+
}
561+
}
562+
563+
impl<I, O> Command for UnregisterSystem<I, O>
564+
where
565+
I: SystemInput + 'static,
566+
O: 'static,
567+
{
568+
fn apply(self, world: &mut World) {
569+
let _ = world.unregister_system(self.system_id);
570+
}
571+
}
572+
547573
/// The [`Command`] type for running a cached one-shot system from
548574
/// [`Commands`](crate::system::Commands).
549575
///
@@ -834,7 +860,7 @@ mod tests {
834860
let new = world.register_system_cached(four);
835861
assert_eq!(old, new);
836862

837-
let result = world.remove_system_cached(four);
863+
let result = world.unregister_system_cached(four);
838864
assert!(result.is_ok());
839865
let new = world.register_system_cached(four);
840866
assert_ne!(old, new);

0 commit comments

Comments
 (0)