Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make standard commands more ergonomic (in niche cases) #8249

Merged
merged 5 commits into from
Mar 29, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ impl<'w, 's> Commands<'w, 's> {
/// # bevy_ecs::system::assert_is_system(initialise_scoreboard);
/// ```
pub fn init_resource<R: Resource + FromWorld>(&mut self) {
self.queue.push(InitResource::<R> {
_phantom: PhantomData::<R>::default(),
});
self.queue.push(InitResource::<R>::new());
}

/// Pushes a [`Command`] to the queue for inserting a [`Resource`] in the [`World`] with a specific value.
Expand Down Expand Up @@ -499,9 +497,7 @@ impl<'w, 's> Commands<'w, 's> {
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn remove_resource<R: Resource>(&mut self) {
self.queue.push(RemoveResource::<R> {
phantom: PhantomData,
});
self.queue.push(RemoveResource::<R>::new());
}

/// Pushes a generic [`Command`] to the command queue.
Expand Down Expand Up @@ -748,10 +744,7 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> {
where
T: Bundle,
{
self.commands.add(Remove::<T> {
entity: self.entity,
phantom: PhantomData,
});
self.commands.add(Remove::<T>::new(self.entity));
self
}

Expand Down Expand Up @@ -942,7 +935,7 @@ where
#[derive(Debug)]
pub struct Remove<T> {
pub entity: Entity,
pub phantom: PhantomData<T>,
pub _phantom: PhantomData<T>,
}

impl<T> Command for Remove<T>
Expand All @@ -956,6 +949,16 @@ where
}
}

impl<T> Remove<T> {
/// Creates a [`Command`] which will remove the specified [`Entity`] when flushed
pub const fn new(entity: Entity) -> Self {
Self {
entity,
_phantom: PhantomData::<T>,
}
}
}

pub struct InitResource<R: Resource + FromWorld> {
_phantom: PhantomData<R>,
}
Expand All @@ -966,6 +969,15 @@ impl<R: Resource + FromWorld> Command for InitResource<R> {
}
}

impl<R: Resource + FromWorld> InitResource<R> {
/// Creates a [`Command`] which will insert a default created [`Resource`] into the [`World`]
pub const fn new() -> Self {
Self {
_phantom: PhantomData::<R>,
}
}
}

pub struct InsertResource<R: Resource> {
pub resource: R,
}
Expand All @@ -977,7 +989,7 @@ impl<R: Resource> Command for InsertResource<R> {
}

pub struct RemoveResource<R: Resource> {
pub phantom: PhantomData<R>,
pub _phantom: PhantomData<R>,
}

impl<R: Resource> Command for RemoveResource<R> {
Expand All @@ -986,6 +998,15 @@ impl<R: Resource> Command for RemoveResource<R> {
}
}

impl<R: Resource> RemoveResource<R> {
/// Creates a [`Command`] which will remove a [`Resource`] from the [`World`]
fn new() -> Self {
RobWalt marked this conversation as resolved.
Show resolved Hide resolved
Self {
_phantom: PhantomData::<R>,
}
}
}

/// [`Command`] to log the components of a given entity. See [`EntityCommands::log_components`].
pub struct LogComponents {
entity: Entity,
Expand Down