Skip to content

AssetsInsert effect #126

@Trouv

Description

@Trouv

For adding an asset to the asset store with an existing handle.

#[derive(Default, Debug, PartialEq, Eq, Clone, Hash, Reflect)]
pub struct AssetsInsert<A>
where
    A: Asset,
{
    pub handle: Handle<A>,
    pub asset: A,
}

/// Construct a new [`AssetsInsert`] [`Effect`].
pub fn assets_insert<A: Asset>(handle: Handle<A>, asset: A) -> AssetsInsert<A> {
    AssetsInsert { handle, asset }
}

impl<A> Effect for AssetsInsert<A>
where
    A: Asset,
{
    type MutParam = (
        ResMut<'static, Assets<A>>,
        <Result<(), InvalidGenerationError> as Effect>::MutParam,
    );

    fn affect(self, param: &mut <Self::MutParam as bevy::ecs::system::SystemParam>::Item<'_, '_>) {
        match param.0.insert(&self.handle, self.asset) {
            Ok(()) => (),
            e => e.affect(&mut param.1),
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions