Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading and using a font after startup can lead to panic. #489

Closed
BorisBoutillier opened this issue Sep 14, 2020 · 3 comments
Closed

Loading and using a font after startup can lead to panic. #489

BorisBoutillier opened this issue Sep 14, 2020 · 3 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds P-Crash A sudden unexpected crash

Comments

@BorisBoutillier
Copy link
Contributor

If you load asynchronous a font through the assert_server and directly use the font_handle, panic can happen if this is done in a regular system, after startup.
I have modified the text.rs to show the panic. Modification is that the commands and asset loading are not done at startup but on a specific frame number.

Here is the modified example:

use bevy::{
    diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
    prelude::*,
};

/// This example illustrates how to create text and update it in a system. It displays the current FPS in the upper left hand corner.
fn main() {
    App::build()
        .add_default_plugins()
        .add_plugin(FrameTimeDiagnosticsPlugin::default())
        .add_resource(1u32)
        .add_system(setup.system())
        .add_system(text_update_system.system())
        .run();
}
fn text_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text>) {
    for mut text in &mut query.iter() {
        if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
            if let Some(average) = fps.average() {
                text.value = format!("FPS: {:.2}", average);
            }
        }
    }
}

const FRAME_SPAWN: u32 = 3;

fn setup(mut commands: Commands, count: Res<u32>, asset_server: Res<AssetServer>) {
    if *count <= FRAME_SPAWN {
        println!("FRAME {}", *count);
        if *count == FRAME_SPAWN {
            let font_handle = asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap();
            commands
                // 2d camera
                .spawn(UiCameraComponents::default())
                // texture
                .spawn(TextComponents {
                    style: Style {
                        align_self: AlignSelf::FlexEnd,
                        ..Default::default()
                    },
                    text: Text {
                        value: "FPS:".to_string(),
                        font: font_handle,
                        style: TextStyle {
                            font_size: 60.0,
                            color: Color::WHITE,
                        },
                    },
                    ..Default::default()
                });
        }
        commands.insert_resource(*count + 1);
    }
}

Here is the panic message :

thread 'Compute Task Pool (2)' panicked at 'called `Option::unwrap()` on a `None` value', crates/bevy_text/src/font_atlas_set.rs:53:42
@BorisBoutillier
Copy link
Contributor Author

I think the issue is that some code in bevy_text and bevy_ui is doing fonts.get(font_handle).unwrap(), but because asset loading is asynchronous, the Assets is not yet filled with the associated font.

I'm preparing a PR for that.

@BorisBoutillier
Copy link
Contributor Author

Note that the same panic will happen by just modifying the original text.rs example and pointing to a non-existent font path.

@Moxinilian Moxinilian added A-Assets Load files from disk to use for things like images, models, and sounds P-Crash A sudden unexpected crash labels Sep 14, 2020
@BorisBoutillier
Copy link
Contributor Author

Fixed by #490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds P-Crash A sudden unexpected crash
Projects
None yet
Development

No branches or pull requests

2 participants