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

Implement DetectChanges for tuples and derived SystemParams #13392

Open
wainwrightmark opened this issue May 16, 2024 · 2 comments
Open

Implement DetectChanges for tuples and derived SystemParams #13392

wainwrightmark opened this issue May 16, 2024 · 2 comments
Labels
A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes X-Contentious There are nontrivial implications that should be thought through

Comments

@wainwrightmark
Copy link
Contributor

What problem does this solve or what need does it fill?

At the moment DetectChanges is implemented for Res<T>, ResMut<T>, and some other system parameters but not for tuples thereof.

I've rolled some of my own generic system parameters which use DetectChanges and the lack of tuple support is very annoying.

For example I have Cached<R: ReadOnlySystemParam + DetectChanges, V> which gives you a value V which is automatically recalculated only if R changes (this is useful if recalculating the value is expensive). This works when R is a single resource but it would be lovely if it could also work for tuples of resources and types with `#[derive(SystemParam)]

I have a feeling that the reason this hasn't been done already is that DetectChanges has the last_changed(&self) method which I'm not sure is possible to calculate for tuples. It would seem trivial - just take the max of the member values - but the way Tick works is that it wraps around at a certain number so the highest value is not necessarily the most recent.

What solution would you like?

The outcome I would like is to be able to call is_changed() on tuples like (Res<T>, ResMut<U>) and similar, more complex nested tuples and appropriate types with #[derive(SystemParam)]

I don't think there's a way to do this without breaking changes (or an incorrect last_changed implementation) but I think the best way to do this would be to split DetectChanges into two traits: one for is_added and is_changed and the other for last_changed.

What alternative(s) have you considered?

  • I could be wrong and there is in fact some clever way to determine which member of a set of ticks is the most recent
  • The signature of last_changed could be changed to take the current tick of the system and that could be used as a reference to deal with the wraparound.
  • I can stick with my current solution which is to have my own DetectChanges2 trait that I've implemented for the types I need. This is quite annoying though as it leads to some extra boilerplate.
@wainwrightmark wainwrightmark added C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels May 16, 2024
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events X-Contentious There are nontrivial implications that should be thought through D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes C-Usability A simple quality-of-life change that makes Bevy easier to use and removed C-Enhancement A new feature S-Needs-Triage This issue needs to be labelled labels May 16, 2024
@alice-i-cecile
Copy link
Member

We've been strongly considering swapping to simple u64 ticks for other reasons: that would make this much simpler for you.

@wainwrightmark
Copy link
Contributor Author

We've been strongly considering swapping to simple u64 ticks for other reasons: that would make this much simpler for you.

Oh nice. Yes that would make this pretty easy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A simple quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

No branches or pull requests

2 participants