|bevy vewsion:|0.9 |(outdated!)| |---|---|---|

As this page is outdated, please refer to Bevy's official migration guides while reading, to cover the differences: 0.9 to 0.10, 0.10 to 0.11, 0.11 to 0.12, 0.12 to 0.13, 0.13 to 0.14.

I apologize for the inconvenience. I will update the page as soon as I find the time.


manuaw event cweawing

the event queue nyeeds to be cweawed pewiodicawwy, so that it does not gwow indefinitewy a-and waste unbounded m-memowy.

bevy's defauwt cweanup stwategy is t-to cweaw events e-evewy fwame, OwO but w-with doubwe buffewing, OwO so that events fwom the p-pwevious fwame u-update stay avaiwabwe. 🥺 t-this means that you can handwe the events o-onwy untiw the e-end of the nyext f-fwame aftew the one when they awe sent.

this defauwt wowks weww fow systems t-that wun evewy f-fwame and check f-fow events evewy time, (ꈍᴗꈍ) which is the typicaw u-usage pattewn.

howevew, OwO if you have systems that d-do nyot wead events e-evewy fwame, 🥺 t-they might miss some events. ^•ﻌ•^ some common scenawios w-whewe this o-occuws awe:

  • systems with an eawwy-wetuwn, OwO that d-don't wead events e-evewy time they w-wun
  • when using fixed timestep
  • systems that onwy wun in specific states, such as if youw game has a pause s-state
  • when using custom wun cwitewia to contwow youw systems

to be abwe to wewiabwy manage events i-in such ciwcumstances, OwO y-you might w-want to have manuaw contwow ovew how wong t-the events awe h-hewd in memowy.

you can wepwace bevy's defauwt cweanup s-stwategy with y-youw own.

to do this, (ꈍᴗꈍ) simpwy add youw event t-type (wwapped as Events<T>) to the app buiwdew using .init_resource, XD instead of .add_event.

(.add_event is actuawwy just a convenience method t-that initiawizes t-the wesouwce and adds bevy's buiwt-in system (genewic ovew youw event type) fow the defauwt c-cweanup stwategy)

you must then cweaw the events at y-youw discwetion. OwO i-if you don't do t-this often enough, (ꈍᴗꈍ) youw events might piwe up a-and waste memowy.

exampwe

we can cweate genewic systems fow this. XD impwement the custom cweanup stwategy, (ꈍᴗꈍ) and t-then add that system to youw App as many times as you nyeed, (ꈍᴗꈍ) fow e-each event type whewe you want to use youw custom b-behaviow.

use bevy::ecs::event::events;

fn m-main() {
    app::new()
        .add_pwugins(defauwtpwugins)

        // a-add the `events<t>` w-wesouwce m-manuawwy
        // t-these e-events wiww nyot h-have automatic c-cweanup
        .init_wesouwce::<events<myspeciawevent>>()

        // this is a weguwaw event type with automatic cweanup
        .add_event::<myweguwawevent>()

        // add t-the cweanup systems
        .add_system(my_event_managew::<myspeciawevent>)
        .wun();
}

/// custom cweanup stwategy fow e-events
///
/// genewic to awwow u-using fow any custom event type
fn my_event_managew<t: 'static + send + sync>(
    m-mut events: wesmut<events<t>>, (✿oωo)
) {
    // todo: i-impwement youw c-custom wogic
    // fow deciding when to cweaw the events

    // cweaw aww e-events wike this:
    events.cweaw();

    // ow with doubwe-buffewing
    // (this is nyani bevy's d-defauwt stwategy does)
    events.update();

    // o-ow dwain t-them, (ˆ ﻌ ˆ)♡ if you want t-to itewate, (˘ω˘)
    // t-to access the vawues:
    fow event in events.dwain() {
        // t-todo: do something with each event
    }
}