bevy icon indicating copy to clipboard operation
bevy copied to clipboard

`ReflectComponent` Commands

Open PROMETHIA-27 opened this issue 3 years ago • 0 comments

(Note: Primary changes are in bevy_ecs/src/reflect.rs, almost every other file just has FromReflect derives added)

Objective

Currently, ReflectComponent (and ReflectResource) contain a series of functions that require a &mut World in order to use them. This severely affects the parallelism of using these reflection tools and limits user's interest in them. It also leads to more convoluted code when having to request a lot of resources for a system that could otherwise take advantage of the SystemParam system. This PR is an attempt to improve this API by adding functions to ReflectComponent and ReflectResource that take Commands instead of &mut World.

Solution

The most significant change that needs to happen to support this API update is that instead of implementing FromType<C> for ReflectComponent/Resource for C: Component + Reflect + FromWorld, we implement for C: Component + Reflect + FromReflect. In some ways this might be considered more permissive, but in others more restrictive. Some types may be constructible with FromWorld but not FromReflect, and vice versa. I believe that overall this change is more permissive than not, especially as types that cannot be constructed from a reflected value are more likely to behave unintuitively in reflection-heavy systems. A more impactful issue with this is that now many types will need #[derive(FromReflect)] added in order to work with ReflectComponent/Resource, which may be addressed in a different PR (opt-out FromReflect?).


Changelog

Added:

  • insert_command function to ReflectComponent
  • FromReflect derives to roughly 50-100 bevy types which used #[reflect(Component)]

Changed:

  • FromWorld bound on ReflectComponent's FromType impl to FromReflect

Migration Guide

  • If a type that previously used #[reflect(Component)] or #[reflect(Resource)] does not have #[derive(FromReflect)], it needs that added. A small amount of complex types may not be capable of this, and will need to be changed.

PROMETHIA-27 avatar Aug 01 '22 20:08 PROMETHIA-27