-
Hi! I'm relatively new to bevy and ECS in general - I'm trying to display a player name followed by a health bar, resulting in something looking like Example Code (using version use bevy::prelude::*;
#[derive(Bundle)]
struct PlayerHealthBundle
{
name: TextBundle,
health_bar: NodeBundle,
}
fn example_startup_system(mut commands: Commands)
{
commands.spawn(Camera2dBundle::default());
commands.spawn(PlayerHealthBundle {
name: TextBundle::from_section(
"Player: ",
TextStyle { font_size: 10.0, color: Color::WHITE, ..default() }
),
health_bar: NodeBundle {
style: Style {
width: Val::Px(100.0),
height: Val::Px(20.0),
..default()
},
background_color: BackgroundColor(Color::GREEN),
..default()
}
});
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, example_startup_system)
.run();
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In this particular case, you don't need the But for future references, you can't really create a bundle this way, your best bet is to insert the bundles separately, using a chain of When you actually need to avoid duplicate components in bundles commands.spawn(TextBundle::from_section(
"Player: ",
TextStyle { font_size: 10.0, color: Color::WHITE, ..default() }
))
.insert(NodeBundle {
style: Style {
width: Val::Px(100.0),
height: Val::Px(20.0),
..default()
},
background_color: BackgroundColor(Color::GREEN),
..default()
}) |
Beta Was this translation helpful? Give feedback.
In this particular case, you don't need the
NodeBundle
, sinceTextBundle
already includes all the necessary components.But for future references, you can't really create a bundle this way, your best bet is to insert the bundles separately, using a chain of
.insert()
as follow:When you actually need to avoid duplicate components in bundles