Iterating over changed components
- Lingua principale
- Rust
- Stelle
- 1.7k
- Fork
- 140
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
The current change events / filters arent useful for optimizations. Sometimes I only want to run a system on a component if its value has _actually changed_. For example, updating a transform when the position changes, re-rendering text when the text string changes, etc.
EDIT (for clarity): Currently components are considered "changed" whenever any query includes `Write`. The "changed" state is set even if the component wasn't actually modified in the last update:
```rust
// changed state is set on every update, even though it is only changed every 10 minutes
for x in >::query().iter_mut(world) {
if 10_minutes_have_passed {
x.value += 1;
}
}
```
One solution would be to add setters to every component that needs tracking like this, then manually filter during iteration. However this seems like the kind of cross cutting concern that should implemented within the ECS. I think we already have most of the building blocks required to make it happen.
This seems possible by adding a "modified" flag to `RefMut`. Any time `RefMut` is mutably derefed (`DerefMut` trait impl), the modified flag would be set to true. `Query`'s internal iterator implementation could then read the flag on each iteration and store the modified state somewhere (either by sending an event or writing the state to the component storage). Then a "modified component" iterator could read that state and use it as a filter.
The state would need to be cleared at the end of an "update". Given that this could be app specific, its probably best to leave clearing the state as an exercise for the user.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.