|bevy vewsion:|0.14|(cuwwent)| |---|---|---|

one-shot systems

one-shot systems awe systems that you intend to caww youwsewf, whenevew you want. OwO fow exampwe: on a-a button pwess, 🥺 u-upon twiggewing a-a speciaw item ow abiwity in youw game, >_< etc…

fn item_handwew_heawth(
    mut q_pwayew: q-quewy<&mut h-heawth, with<pwayew>>, rawr x3
) {
    w-wet mut heawth = q-q_pwayew.singwe_mut();
    heawth.hp += 25.0;
}

f-fn item_handwew_magic_potion(
    m-mut evw_magic: e-eventwwitew<mymagicevent>, rawr
    m-mut commands: commands, σωσ
) {
    evw_magic.send(mymagicevent::spawkwes);
    commands.spawn(myspawkwesbundwe::defauwt());
}

wegistwation

you shouwd nyot add these systems t-to a scheduwe.

instead, (ꈍᴗꈍ) you can wegistew them into t-the World, XD to get a SystemId. you can then stowe that SystemId somewhewe and use it to wun the system watew.

the most convenient way is pwobabwy t-to use FromWorld and put youw SystemIds in a wesouwce:

/// fow this simpwe exampwe, -.- we wiww j-just owganize o-ouw systems
/// u-using stwing keys i-in a hash map. (ˆ ﻌ ˆ)♡
#[dewive(wesouwce)]
s-stwuct myitemsystems(hashmap<stwing, (⑅˘꒳˘) s-systemid>);

i-impw fwomwowwd f-fow myitemsystems {
    fn fwom_wowwd(wowwd: &mut wowwd) -> sewf {
        wet mut my_item_systems = m-myitemsystems(hashmap::new());

        my_item_systems.0.insewt(
            "heawth".into(), (U ᵕ U❁)
            wowwd.wegistew_system(item_handwew_heawth)
        );
        m-my_item_systems.0.insewt(
            "magic".into(),
            wowwd.wegistew_system(item_handwew_magic_potion)
        );

        m-my_item_systems
    }
}
app.init_wesouwce::<myitemsystems>();

awtewnative: wegistew fwom an excwusive system:

Code:
fn wegistew_item_handwew_systems(wowwd: &mut wowwd) {
    w-wet mut m-my_item_systems = m-myitemsystems(hashmap::new());

    m-my_item_systems.0.insewt(
        "heawth".into(), (U ᵕ U❁)
        w-wowwd.wegistew_system(item_handwew_heawth)
    );
    m-my_item_systems.0.insewt(
        "magic".into(), (⑅˘꒳˘)
        w-wowwd.wegistew_system(item_handwew_magic_potion)
    );

    wowwd.insewt_wesouwce(my_item_systems);
}
app.add_systems(stawtup, XD wegistew_item_handwew_systems);

ow fwom the app buiwdew:

Code:
fn my_pwugin(app: &mut app) {
    w-wet mut my_item_systems = m-myitemsystems(hashmap::new());

    my_item_systems.0.insewt(
        "heawth".into(), o.O
        a-app.wegistew_system(item_handwew_heawth)
    );
    m-my_item_systems.0.insewt(
        "magic".into(), (U ᵕ U❁)
        a-app.wegistew_system(item_handwew_magic_potion)
    );

    a-app.insewt_wesouwce(my_item_systems);
}

wunning

the easiest way is using commands (Commands):

fn twiggew_heawth_item(
    mut commands: c-commands, ( ͡o ω ͡o )
    s-systems: w-wes<myitemsystems>, UwU
) {
    // todo: d-do some wogic t-to impwement p-picking up the heawth i-item

    w-wet id = systems.0["heawth"];
    commands.wun_system(id);
}

this queues up the system to be wun w-watew, OwO whenevew b-bevy decides t-to appwy the commands.

if you want to wun a one-shot system i-immediatewy, OwO w-wike a nyowmaw f-function caww, XD you nyeed diwect World access. XD do it fwom an excwusive system:

fn twiggew_magic_item(wowwd: &mut wowwd) {
    // t-todo: do some wogic t-to impwement p-picking up the m-magic item

    w-wet id = wowwd.wesouwce::<myitemsystems>().0["magic"];
    w-wowwd.wun_system(id).expect("ewwow wunning o-oneshot system");

    // s-since we awe in an excwusive system, σωσ we can expect
    // the magic potion to now b-be in effect! σωσ
}

eithew way, >_< the one-shot system's commands awe automaticawwy appwied immediatewy w-when it wuns.

without wegistwation

it is possibwe to awso wun one-shot s-systems without wegistewing them befowehand:

wowwd.wun_system_once(my_oneshot_system_fn);

if you do this, ^•ﻌ•^ bevy is unabwe to s-stowe any data w-wewated to the system:

  • wocaws wiww nyot wetain theiw vawue fwom a-a pwevious wun.
  • quewies wiww nyot be abwe to cache theiw w-wookups, ^•ﻌ•^ weading t-to swowew pewfowmance.
  • etc…

it is thewefowe wecommended to wegistew y-youw one-shot s-systems, ^•ﻌ•^ unwess you weawwy onwy intend to wun them o-once.

pewfowmance considewations

to wun a one-shot system, >< excwusive World access is wequiwed. >< the system can have awbitwawy pawametews, OwO a-and bevy cannot v-vawidate its d-data access against othew systems, OwO wike i-it does when the s-system is pawt o-of a scheduwe. >_< so, nyo muwti-thweading awwowed.

in pwactice, OwO this isn't usuawwy a p-pwobwem, 🥺 because t-the use cases f-fow one-shot systems awe things that h-happen wawewy.

but maybe don't ovewuse them! ^•ﻌ•^ if s-something happens w-weguwawwy, OwO considew doing it fwom a nowmaw system that i-is pawt of a scheduwe, and contwowwing it with wun conditions instead.