Function moonshine_check::repair

source ·
pub fn repair(f: impl Fix) -> Policy
Expand description

Returns a Policy which tries to repair matching instances.

§Usage

Use this policy if the matching instances can be repaired by inserting or removing components. This is especially useful to handle backwards compatibility when loading from saved data.

§Example

use bevy::prelude::*;
use moonshine_check::prelude::*;

#[derive(Bundle, Default)]
struct AB {
   a: A,
   b: B,
}

#[derive(Component, Default)]
struct A;

#[derive(Component, Default)]
struct B;

let mut app = App::new();
app.add_plugins(MinimalPlugins)
    .check::<A, Without<B>>(repair(|entity: EntityRef, commands: &mut Commands| {
        commands.entity(entity.id()).insert(B);
    }));

app.world_mut().spawn(A); // Bug! `B` is missing!
app.update();

fn update(items: Query<Entity, With<A>>, query: Query<&B>) {
    for entity in items.iter() {
        // Guaranteed:
        assert!(query.contains(entity));
    }
}