Enable dynamic mutable access to component data #1284
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm working on developing a world visualizer/editor for Bevy. See a demo: https://discord.com/channels/691052431525675048/692648638823923732/801608361463513089
To work, the tool needs to get mutable access to every component in the world, as well as find a method for displaying the component in a UI. Right now, I'm using this strategy:
TypeId
to pre-registered types that implement theInspectable
trait inbevy-inspector-egui
(see here). For these types, I usearchetype.get_dynamic
to get the component data as*mut u8
then transmute it to the appropriate type.Inspectable
is not implemented, then try to cast the component to&mut dyn Reflect
usingReflectComponent
, then usebevy-inspector-egui
to generate a generic UI for components implementing Reflect.This PR adds two changes to make this possible. First, it publicly exposes
Archetype::get_dynamic
. Second, it adds a new methodReflectComponent::reflect_component_mut
that returns&mut dyn Reflect
instead of just&dyn Reflect
.Note: obviously the proliferation of unsafe code and transmute is concerning here. If there's a different approach that works without these changes, I'd be happy to hear.