|
| 1 | +# B0004 |
| 2 | + |
| 3 | +A runtime warning. |
| 4 | + |
| 5 | +An [`Entity`] with a hierarchy-inherited component has a [`Parent`] |
| 6 | +without the hierarchy-inherited component in question. |
| 7 | + |
| 8 | +The hierarchy-inherited components are: |
| 9 | + |
| 10 | +- [`ComputedVisibility`] |
| 11 | +- [`GlobalTransform`] |
| 12 | + |
| 13 | +For example, the following code will cause a warning to be emitted: |
| 14 | + |
| 15 | +```rust,no_run |
| 16 | +use bevy::prelude::*; |
| 17 | +
|
| 18 | +// WARNING: this code is buggy |
| 19 | +fn setup_cube( |
| 20 | + mut commands: Commands, |
| 21 | + mut meshes: ResMut<Assets<Mesh>>, |
| 22 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 23 | +) { |
| 24 | + commands |
| 25 | + .spawn_bundle(TransformBundle::default()) |
| 26 | + .with_children(|parent| { |
| 27 | + // cube |
| 28 | + parent.spawn_bundle(PbrBundle { |
| 29 | + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), |
| 30 | + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), |
| 31 | + transform: Transform::from_xyz(0.0, 0.5, 0.0), |
| 32 | + ..default() |
| 33 | + }); |
| 34 | + }); |
| 35 | +
|
| 36 | + // camera |
| 37 | + commands.spawn_bundle(Camera3dBundle { |
| 38 | + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 39 | + ..default() |
| 40 | + }); |
| 41 | +} |
| 42 | +
|
| 43 | +fn main() { |
| 44 | + App::new() |
| 45 | + .add_plugins(DefaultPlugins) |
| 46 | + .add_startup_system(setup_cube) |
| 47 | + .run(); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +This code **will not** show a cube on screen. |
| 52 | +This is because the entity spawned with `commands.spawn_bundle(…)` |
| 53 | +doesn't have a [`ComputedVisibility`] component. |
| 54 | +Since the cube is spawned as a child of an entity without the |
| 55 | +[`ComputedVisibility`] component, it will not be visible at all. |
| 56 | + |
| 57 | +To fix this, you must use [`SpatialBundle`] over [`TransformBundle`], |
| 58 | +as follow: |
| 59 | + |
| 60 | +```rust,no_run |
| 61 | +use bevy::prelude::*; |
| 62 | +
|
| 63 | +fn setup_cube( |
| 64 | + mut commands: Commands, |
| 65 | + mut meshes: ResMut<Assets<Mesh>>, |
| 66 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 67 | +) { |
| 68 | + commands |
| 69 | + // We use SpatialBundle instead of TransformBundle, it contains the |
| 70 | + // ComputedVisibility component needed to display the cube, |
| 71 | + // In addition to the Transform and GlobalTransform components. |
| 72 | + .spawn_bundle(SpatialBundle::default()) |
| 73 | + .with_children(|parent| { |
| 74 | + // cube |
| 75 | + parent.spawn_bundle(PbrBundle { |
| 76 | + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), |
| 77 | + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), |
| 78 | + transform: Transform::from_xyz(0.0, 0.5, 0.0), |
| 79 | + ..default() |
| 80 | + }); |
| 81 | + }); |
| 82 | +
|
| 83 | + // camera |
| 84 | + commands.spawn_bundle(Camera3dBundle { |
| 85 | + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 86 | + ..default() |
| 87 | + }); |
| 88 | +} |
| 89 | +
|
| 90 | +fn main() { |
| 91 | + App::new() |
| 92 | + .add_plugins(DefaultPlugins) |
| 93 | + .add_startup_system(setup_cube) |
| 94 | + .run(); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +A similar problem occurs when the [`GlobalTransform`] component is missing. |
| 99 | +However, when a parent [`GlobalTransform`] is missing, |
| 100 | +it will simply prevent all transform propagation, |
| 101 | +including when updating the [`Transform`] component of the child. |
| 102 | + |
| 103 | +You will most likely encouter this warning when loading a scene |
| 104 | +as a child of a pre-existing [`Entity`] that does not have the proper components. |
| 105 | + |
| 106 | +[`ComputedVisibility`]: https://docs.rs/bevy/*/bevy/render/view/struct.ComputedVisibility.html |
| 107 | +[`GlobalTransform`]: https://docs.rs/bevy/*/bevy/transform/components/struct.GlobalTransform.html |
| 108 | +[`Transform`]: https://docs.rs/bevy/*/bevy/transform/components/struct.Transform.html |
| 109 | +[`Parent`]: https://docs.rs/bevy/*/bevy/hierarchy/struct.Parent.html |
| 110 | +[`Entity`]: https://docs.rs/bevy/*/bevy/ecs/entity/struct.Entity.html |
| 111 | +[`SpatialBundle`]: https://docs.rs/bevy/*/bevy/render/prelude/struct.SpatialBundle.html |
| 112 | +[`TransformBundle`]: https://docs.rs/bevy/*/bevy/transform/struct.TransformBundle.html |
0 commit comments