Skip to content

Loading an asset and one of its subassets causes two loads of the same asset to occur. #12756

@andriyDev

Description

@andriyDev

Bevy version

Commit: 6840f95

Loading an asset as well as one of its subassets causes the loader to run twice. This also causes asset events to trigger twice, and causes assets to be added to Assets and then immediately overwritten.

Here is an example that demonstrates the issue:

use bevy::{gltf::Gltf, prelude::*};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, (print_mesh_events, print_gltf_events))
        .run();
}

#[derive(Resource)]
struct Handles {
    gltf: Handle<Gltf>,
    mesh: Handle<Mesh>,
}

fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
    commands.insert_resource(Handles {
        gltf: asset_server.load("scene.gltf"),
        mesh: asset_server.load("scene.gltf#Mesh0/Primitive0"),
    });
}

fn print_mesh_events(mut events: EventReader<AssetEvent<Mesh>>) {
    for event in events.read() {
        dbg!(event);
    }
}

fn print_gltf_events(mut events: EventReader<AssetEvent<Gltf>>) {
    for event in events.read() {
        dbg!(event);
    }
}

This prints that any meshes and the gltf are first added and then modified immediately. This likely means the loader is running twice. At the very least, the loader is running once, but the AssetServer loading code is writing to the Assets twice which can be undesirable.

I believe this is because the loaading code in AssetServer sees the asset is not loaded (for that asset path), so it kicks off a load. But since the paths are technically different, it kicks off two loads.

I also tested this with loading two subassets of the same asset, and it also kicks off two loads! Note this doesn't happen if the first load finishes before the second subasset is requested.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AssetsLoad files from disk to use for things like images, models, and soundsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions