|
| 1 | +//! This example demonstrates how reflection trait objects can be nested in other reflected types |
| 2 | +//! using remote reflection. |
| 3 | +
|
| 4 | +use bevy::prelude::*; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + // Bevy's reflection crate relies heavily on the `dyn Reflect` trait object. |
| 8 | + // This allows the compile-time name of a type to be "erased" and passed around at runtime, |
| 9 | + // most often as a `Box<dyn Reflect>`. |
| 10 | + let _: Box<dyn Reflect> = Box::new(String::from("Hello, World!")); |
| 11 | + |
| 12 | + // However, you'll notice that `Box<dyn Reflect>` itself doesn't implement `Reflect`. |
| 13 | + // This makes it impossible to use `Box<dyn Reflect>` as a field in a struct that derives `Reflect`. |
| 14 | + // ``` |
| 15 | + // #[derive(Reflect)] |
| 16 | + // struct MyType { |
| 17 | + // field: Box<dyn Reflect>, // <- Compile Error |
| 18 | + // } |
| 19 | + // ``` |
| 20 | + // This is because it would be too easy to accidentally box a `Reflect` type, |
| 21 | + // then accidentally box it again, and again, and so on. |
| 22 | + // So instead, `bevy_reflect` exposes a `ReflectBox` type which can be used |
| 23 | + // as a remote wrapper around a `Box<dyn Reflect>` (or `Box<dyn PartialReflect>`). |
| 24 | + // |
| 25 | + // For example, let's say we want to define some equipment for a player. |
| 26 | + // We don't know what kind of equipment the player will have at compile time, |
| 27 | + // so we want to store it as a `Box<dyn Reflect>`. |
| 28 | + // To do this, we first need to derive `Reflect` for our `Player`. |
| 29 | + #[derive(Reflect)] |
| 30 | + // Next, we need to opt out of deriving `FromReflect` since `Box<dyn Reflect>` |
| 31 | + // has no knowledge of `FromReflect`. |
| 32 | + #[reflect(from_reflect = false)] |
| 33 | + struct Player { |
| 34 | + // Now we can use remote reflection to tell `Reflect` how to reflect our `Box<dyn Reflect>`. |
| 35 | + #[reflect(remote = bevy::reflect::boxed::ReflectBox<dyn Reflect>)] |
| 36 | + equipment: Box<dyn Reflect>, |
| 37 | + } |
| 38 | + |
| 39 | + // Now we can use any type that implements `Reflect` as equipment for our player. |
| 40 | + let equipment: Box<dyn Reflect> = Box::new(String::from("Sword")); |
| 41 | + let mut player: Box<dyn Struct> = Box::new(Player { equipment }); |
| 42 | + |
| 43 | + // We can also use reflection to modify our player's equipment. |
| 44 | + let equipment = player.field_mut("equipment").unwrap(); |
| 45 | + equipment.try_apply(&String::from("Shield")).unwrap(); |
| 46 | + assert!(player |
| 47 | + .reflect_partial_eq(&Player { |
| 48 | + equipment: Box::new(String::from("Shield")), |
| 49 | + }) |
| 50 | + .unwrap_or_default()); |
| 51 | +} |
0 commit comments