mrk-its / bevy_webgl2

WebGL2 renderer plugin for Bevy game engine
MIT License
172 stars 46 forks source link

Text doesn't work if bevy is started within an async function #50

Open johanhelsing opened 2 years ago

johanhelsing commented 2 years ago

Maybe the title is a bit inaccurate, but I had trouble rendering text in my game. I looked at the network requests, and saw that the font assets were never requests. Weirdly enough, moving the App::run call out of the async function where I normally start bevy seems to fix the issue.

I.e. this is what I did before:

fn main() {
    wasm_bindgen_futures::spawn_local(async move {
        main_async().await.expect("main failed");
    })
}

async fn main_async() -> Result<(), Box<dyn std::error::Error>> {
    let mut app = App::new();
    app.add_plugins(bevy_webgl2::DefaultPlugins);
    app.add_startup_system(lobby_startup.system()).run();
    Ok(())
}

And this is what works:

fn main() {
    let mut app = App::new();
    app.add_plugins(bevy_webgl2::DefaultPlugins);
    app.add_startup_system(lobby_startup.system()).run();
}

I guess that perhaps running App::run(), which is a blocking call, inside an async function is probably making some other async task unhappy... The only problem is, I don't really understand how to solve this. How can I do async setup before starting bevy if bevy can't be started from an async task and webgl2 doesn't support proper threads?