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

Asset server load state not working for individual assets #1184

Closed
willcrichton opened this issue Jan 1, 2021 · 3 comments
Closed

Asset server load state not working for individual assets #1184

willcrichton opened this issue Jan 1, 2021 · 3 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior

Comments

@willcrichton
Copy link
Contributor

willcrichton commented Jan 1, 2021

Bevy version

0.4.0

Operating system & version

OSX

What you did

Patterned after this example, I tried to make an on-load event for a gLTF scene. The code:

use bevy::{asset::LoadState, prelude::*};

fn main() {
  App::build()
    .init_resource::<AssetHandles>()
    .add_plugins(DefaultPlugins)
    .add_resource(State::new(AssetState::Loading))
    .add_stage_after(stage::UPDATE, "assets", StateStage::<AssetState>::default())
    .on_state_enter("assets", AssetState::Loading, load_assets.system())
    .on_state_update("assets", AssetState::Loading, check_assets.system())
    .on_state_enter("assets", AssetState::Finished, init_assets.system())
    .run();
}

#[derive(Clone)]
enum AssetState {
  Loading,
  Finished,
}

[derive(Default)]
struct AssetHandles {
  scene: Handle<Scene>,
}

fn load_assets(
  commands: &mut Commands,
  mut asset_handles: ResMut<AssetHandles>,
  asset_server: Res<AssetServer>,
) {
  asset_handles.scene = asset_server.load("models/Monkey.gltf#Scene0");
  commands.spawn_scene(asset_handles.scene.clone());  
}

fn check_assets(
  mut state: ResMut<State<AssetState>>,
  asset_handles: ResMut<AssetHandles>,
  asset_server: Res<AssetServer>,
) {
  if let LoadState::Loaded = asset_server.get_load_state(&asset_handles.scene) {
    state.set_next(AssetState::Finished).unwrap();
  }
}

fn init_assets(
  asset_handles: Res<AssetHandles>,
  scenes: ResMut<Assets<Scene>>,
) {
  let scene = scenes.get(&asset_handles.scene).unwrap();
}

What you expected to happen

The scenes.get(...) call should return Some.

What actually happened

The scenes.get(...) call returned None, suggesting that the asset was not actually loaded (or that I'm using the API incorrectly somehow?).

@willcrichton willcrichton changed the title Asset server load state not working for gLTF scenes Asset server load state not working for individual assets Jan 1, 2021
@willcrichton
Copy link
Contributor Author

After some debugging, if I use load_folder and HandleUntyped then the program works as expected. It seems the issue is not with gLTF, but rather loading individual assets vs. a folder of assets.

@mockersf
Copy link
Member

mockersf commented Jan 1, 2021

This is working for me using current master.

Adding the #Scene0 to load a scene from a gltf file is a recent change, and was not needed in bevy 0.4, check the load_gltf.rs example from the release (instead of the current one - you can see the difference)

@Moxinilian Moxinilian added A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior labels Jan 4, 2021
@willcrichton
Copy link
Contributor Author

I think it was a version issue, I was able to get this working.

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 C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants