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

Add is_resource_changed_by_id + is_resource_added_by_id #11012

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Changes from all 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
68 changes: 52 additions & 16 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,8 @@ impl World {
.unwrap_or(false)
}

/// Return's `true` if a resource of type `R` exists and was added since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this return's `false`.
/// Returns `true` if a resource of type `R` exists and was added since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for additions since the system last ran.
Expand All @@ -1196,13 +1196,31 @@ impl World {
pub fn is_resource_added<R: Resource>(&self) -> bool {
self.components
.get_resource_id(TypeId::of::<R>())
.and_then(|component_id| self.storages.resources.get(component_id)?.get_ticks())
.map(|ticks| ticks.is_added(self.last_change_tick(), self.read_change_tick()))
.map(|component_id| self.is_resource_added_by_id(component_id))
.unwrap_or(false)
}

/// Return's `true` if a resource of type `R` exists and was modified since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this return's `false`.
/// Returns `true` if a resource with id `component_id` exists and was added since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for additions since the system last ran.
/// - When called elsewhere, this will check for additions since the last time that [`World::clear_trackers`]
Comment on lines +1207 to +1208
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change "this" to "it", feels easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about that, what do other people think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really is a nitpick and nothing relevant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, totally just a nitpick! I just felt like it would be easier to read, but it might just be me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this :p

/// was called.
pub fn is_resource_added_by_id(&self, component_id: ComponentId) -> bool {
self.storages
.resources
.get(component_id)
.and_then(|resource| {
resource
.get_ticks()
.map(|ticks| ticks.is_added(self.last_change_tick(), self.read_change_tick()))
})
.unwrap_or(false)
}

/// Returns `true` if a resource of type `R` exists and was modified since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for changes since the system last ran.
Expand All @@ -1211,8 +1229,26 @@ impl World {
pub fn is_resource_changed<R: Resource>(&self) -> bool {
self.components
.get_resource_id(TypeId::of::<R>())
.and_then(|component_id| self.storages.resources.get(component_id)?.get_ticks())
.map(|ticks| ticks.is_changed(self.last_change_tick(), self.read_change_tick()))
.map(|component_id| self.is_resource_changed_by_id(component_id))
.unwrap_or(false)
}

/// Returns `true` if a resource with id `component_id` exists and was modified since the world's
/// [`last_change_tick`](World::last_change_tick()). Otherwise, this returns `false`.
///
/// This means that:
/// - When called from an exclusive system, this will check for changes since the system last ran.
/// - When called elsewhere, this will check for changes since the last time that [`World::clear_trackers`]
JMS55 marked this conversation as resolved.
Show resolved Hide resolved
/// was called.
pub fn is_resource_changed_by_id(&self, component_id: ComponentId) -> bool {
self.storages
.resources
.get(component_id)
.and_then(|resource| {
resource
.get_ticks()
.map(|ticks| ticks.is_changed(self.last_change_tick(), self.read_change_tick()))
})
.unwrap_or(false)
}

Expand All @@ -1231,8 +1267,8 @@ impl World {
match self.get_resource() {
Some(x) => x,
None => panic!(
"Requested resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_resource` / `app.init_resource`?
"Requested resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_resource` / `app.init_resource`?
Resources are also implicitly added via `app.add_event`,
and can be added by plugins.",
std::any::type_name::<R>()
Expand All @@ -1255,8 +1291,8 @@ impl World {
match self.get_resource_mut() {
Some(x) => x,
None => panic!(
"Requested resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_resource` / `app.init_resource`?
"Requested resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_resource` / `app.init_resource`?
Resources are also implicitly added via `app.add_event`,
and can be added by plugins.",
std::any::type_name::<R>()
Expand Down Expand Up @@ -1326,8 +1362,8 @@ impl World {
match self.get_non_send_resource() {
Some(x) => x,
None => panic!(
"Requested non-send resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`?
"Requested non-send resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`?
Non-send resources can also be be added by plugins.",
std::any::type_name::<R>()
),
Expand All @@ -1348,8 +1384,8 @@ impl World {
match self.get_non_send_resource_mut() {
Some(x) => x,
None => panic!(
"Requested non-send resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`?
"Requested non-send resource {} does not exist in the `World`.
Did you forget to add it using `app.insert_non_send_resource` / `app.init_non_send_resource`?
Non-send resources can also be be added by plugins.",
std::any::type_name::<R>()
),
Expand Down