3

Hello everyone I am trying write the score in the top left corner of the window but for some reason it is not working. Here is the code I used to spawn the text:

commands
.spawn(TextBundle{
    text: Text{value: "Score:".to_string(),
    font: assets.load("FiraSans-Bold.ttf"),
    style:TextStyle{
        font_size:30.0,
        color:Color::WHITE,
        ..Default::default()},..Default::default()},
    transform: Transform::from_translation(Vec3::new(-380.0,-380.0,2.0)),
    ..Default::default()
})
.with(TextTag);

Window is 800 by 800. Any help is appreciated.

Nakroxis
  • 43
  • 3
  • 1
    Have you had a look at the [UI example in the bevy repo](https://github.com/bevyengine/bevy/blob/master/examples/ui/text.rs)? It looks like there is a position variable in style that you use to position the UI element instead of using the transform. It also looks like you need a UI camera and based on the code you have you may not have one – eric556 Feb 11 '21 at 19:12

2 Answers2

2

You may need to add the CameraUiBundle if you did not yet do it.

commands
    .spawn(CameraUiBundle::default())
    .spawn(TextBundle{
        ...
    })
    .with(TextTag);

You might want to do that in your initial setup system in which you also add the camera.

The Bevy version used when answering was 0.4.

MKroehnert
  • 3,637
  • 2
  • 34
  • 43
  • nice one. I didn't realise that the camera was separate to the main camera. Now I wonder how to display text as part of the scene? – Squirrel Apr 20 '22 at 08:47
1

Another reason (in bevy 0.8) might be that there's no default font in bevy (check out this answer)

What you'd need to do would be to download a .ttf file, and then load it when spawning the text.

TextStyle {
    font_size: 50.0,
    color: Color::WHITE,
    font: asset_server.load("your_font.ttf"),
},
aurelia
  • 493
  • 8
  • 12