bevyengine / bevyengine/bevy

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

Open
#16,376 0 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Feature D-Modest S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## 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`.

## 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()`.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the existing OnReplace and OnInsert hook handling used by insert() when replacing a component. Define the OnReinsert behavior from the issue: it receives both old and new component values, fires only for replacement, and does not overlap with OnMutate; verify the existing replace, insert, removal, and mutation paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.