bevyengine/bevy
bevy_reflect
Runtime reflection for Rust types. The bedrock for scenes, the BRP, the inspector, and any data-driven feature in Bevy. Second-largest crate in the workspace at ~140 source files.
Purpose
bevy_reflect provides a Reflect trait that lets you treat a Rust type as a tagged value at runtime: enumerate its fields, read and write them by name, downcast to the concrete type, and serialize/deserialize without type-specific code. It is the API surface most Bevy systems use to "talk about" data they don't statically know.
Reflected types implement Reflect (usually via #[derive(Reflect)]) and are typically registered with the world's TypeRegistry so the rest of the engine can look them up by TypeId or short name.
Directory layout
crates/bevy_reflect/src/
├── lib.rs # Reflect trait + utilities
├── type_registry.rs # The TypeRegistry resource
├── type_info.rs # Static metadata for reflected types
├── enums/ # Enum reflection
├── structs/ # Tuple and named-struct reflection
├── lists/ # Vec, slices
├── maps/ # HashMap, BTreeMap
├── sets/ # HashSet, BTreeSet
├── func/ # Reflectable functions (DynamicFunction)
├── path/ # Path-based access (`my.field[3].x`)
├── serde/ # Reflect-aware serde
├── std_traits.rs # Reflect impls for std types
├── tuple/ # Tuple support
├── array/ # Fixed-size arrays
└── …bevy_reflect_derive/ (sibling crate) defines #[derive(Reflect)], #[derive(FromReflect)], and the Reflect-aware attribute macros.
Key abstractions
| Type | File | Description |
|---|---|---|
Reflect |
crates/bevy_reflect/src/lib.rs |
The core trait. Every reflected type implements it. |
TypeRegistry |
crates/bevy_reflect/src/type_registry.rs |
A registry of reflected types, looked up by TypeId. |
TypeInfo |
crates/bevy_reflect/src/type_info.rs |
Static metadata: kind (struct/enum/etc), fields, attributes. |
FromReflect |
crates/bevy_reflect/src/from_reflect.rs |
Construct a concrete type from a dyn Reflect. |
DynamicStruct / DynamicEnum / DynamicList / DynamicMap |
crates/bevy_reflect/src/{structs,enums,lists,maps}/ |
Runtime representations used during deserialization. |
ReflectSerializer / ReflectDeserializer |
crates/bevy_reflect/src/serde/ |
Reflect-driven serde codecs. |
ReflectFromPtr |
crates/bevy_reflect/src/from_reflect.rs |
Type-erased pointer reflection (used by bevy_ecs). |
GetPath |
crates/bevy_reflect/src/path/mod.rs |
Path-based access (obj.children[2].name). |
How it works
graph LR
Type[#derive Reflect] --> Macro[bevy_reflect_derive]
Macro --> Impls[impl Reflect, GetField, etc.]
Impls --> Registry[TypeRegistry]
Registry --> Inspector
Registry --> Serde
Registry --> BRP[bevy_remote]
Registry --> Scenes[bevy_scene + bevy_world_serialization]#[derive(Reflect)] generates impls for every reflection trait the type can support (struct fields, enum variants, etc). Type registration adds the type to the world's TypeRegistry along with any #[reflect(...)] data — for example, #[reflect(Component)] registers a ReflectComponent that knows how to insert/remove the type as an ECS component on an entity by name.
Reflection drives:
- Scene save/load in
bevy_sceneandbevy_world_serialization. - The Bevy Remote Protocol in
bevy_remote, which serializes components via reflection. - The
template/BSN machinery inbevy_ecsandbevy_world_serialization. - Runtime type inspectors like
bevy-inspector-egui.
Auto-registration
The reflect_auto_register feature uses inventory to collect every #[derive(Reflect)] type at link time so users don't have to call app.register_type::<...>() for each one. On platforms without inventory support (some embedded), the manual registration path is still available.
Integration points
- Depends on:
bevy_ptr,bevy_utils,bevy_platform,bevy_reflect_derive,serde(optional). - Depended on by:
bevy_ecs(optional),bevy_app,bevy_animation,bevy_asset,bevy_scene,bevy_world_serialization,bevy_remote,bevy_state, … - Public macro:
bevy_reflect::Reflectre-exports the derive.
Entry points for modification
- Adding reflection for a new container type: create a new module under
crates/bevy_reflect/src/<kind>/and implement the relevant traits. - Changing serde format:
crates/bevy_reflect/src/serde/. The deserializer is inde/, serializer inser/. - Changing the path syntax:
crates/bevy_reflect/src/path/. The parser is inpath/parse.rs. - Changing how
#[reflect(Component)]works:crates/bevy_ecs/src/reflect/.
See also
bevy_ecsfor how reflection plugs into components.bevy_sceneandbevy_world_serializationfor the consumers.bevy_remotefor the JSON-RPC consumer.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.