PoOnesNerfect/bevy_bsml

How do I update text in the UI?

Closed this issue · 0 comments

Nevermind, I figured it out:

#[derive(Component)]
struct Hud;

#[derive(Component)]
struct TravelDistanceText;

bsml! {Hud;
    (node class=[W_FULL, H_FULL]) {
        (node) {
            (text labels=[TravelDistanceText]) { "0m" }
        }
    }
}

fn spawn_hud(mut commands: Commands) {
    commands.spawn_bsml(Hud);
}

fn despawn_hud(query: Query<Entity, With<Hud>>, mut commands: Commands) {
    if let Ok(entity) = query.get_single() {
        commands.despawn_bsml(entity);
    }
}

fn update_hud(
    mut query: Query<&mut Text, With<TravelDistanceText>>,
    travel_distance: Res<TravelDistanceMeters>,
) {
    if let Ok(mut text) = query.get_single_mut() {
        text.sections[0].value = format!("{}m", travel_distance.0);
    }
}