Detects default Handle in Bevy

Working on an sprite animation crate in Rust with the Bevy engine, I initialize my sprite entities like so:

commands.spawn((
    SpriteBundle::default(),
    TextureAtlas::default()
));

But how to know that this entity is actually initialized as default within my systems?

Reading the bevy's codebase, the default Handle<T> internally uses a default UUID which can be retrieved through AssetId::default().

So you can know if a component use a default assets with this basic comparison:

pub fn initializes_sprite(
    mut sprites_query: Query<(&mut Handle<Image>, &mut TextureAtlas)>
) {
    for (mut texture, mut atlas) in sprites_query.iter_mut() {
        if texture.id() == AssetId::default() {
            // Initializes the sprite texture
        }

        if atlas.layout.id() == AssetId::default() {
            // Initializes the sprite layout
        }
    }
}

Comments

Be the first to post a comment!

Add a comment

Preview