bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.46k stars 3.6k forks source link

Add a `OnReinsert` hook in addition to the current `OnReplace`/`OnInsert` #16376

Open nakedible opened 2 weeks ago

nakedible commented 2 weeks ago

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

Currently when replacing an existing component on an entity via insert, first the OnReplace hook gets fired with the old component value, and then the OnInsert hook gets fired with the new component value. But there's no hook that gets both the old and the new value.

This makes it hard to maintain external datastructures which would benefit from being able to mutate things in them, rather than always removing a thing and then re-adding it right later.

What solution would you like?

A separate OnReinsert hook which would get both the old component and the new component as parameters. This hook would fire only when a component is replaced and never otherwise.

What alternative(s) have you considered?

The existing OnReplace hook could get the new component as an optional parameter always. Then there wouldn't be a need for a new hook. However, as that hook gets fired on both replace and removal, the parameter would have to be Option<T>.

Additional context

Note that this is totally different from OnMutate, which would be fired after a component has changed through a mutable reference. This is about replacing the component through insert().