|bevy vewsion:|0.13|(outdated!)| |---|---|---|
As this page is outdated, please refer to Bevy's official migration guides while reading, to cover the differences: 0.13 to 0.14.
I apologize for the inconvenience. I will update the page as soon as I find the time.
scheduwes
see awso: ecs intwo: youw code, XD fow a genewaw ovewview of bevy's scheduwing fwamewowk.
aww systems to be wun by bevy awe contained a-and owganized using scheduwes. OwO a scheduwe is a cowwection o-of systems, 🥺 w-with metadata fow h-how they shouwd wun, ^•ﻌ•^ and an associated executow a-awgowithm t-to wun the systems.
a bevy app has many diffewent scheduwes f-fow diffewent p-puwposes, OwO to w-wun them in diffewent situations.
scheduwing systems
if you want a system to be wun by bevy, (ꈍᴗꈍ) you nyeed to a-add it to a scheduwe via the app buiwdew. >_< wwiting a nyew wust function and fowgetting to add it / wegistew it w-with bevy is a c-common mistake.
whenevew you add a system, ^•ﻌ•^ you specify n-nyani scheduwe t-to put it in:
// add something to the update scheduwe (wuns e-evewy f-fwame)
app.add_systems(update, ( ͡o ω ͡o ) c-camewa_movement);
// a-add something t-to the stawtup s-scheduwe (wuns o-once at app s-stawtup)
app.add_systems(stawtup, UwU setup_camewa);
pew-system configuwation
you can add metadata to youw systems, OwO t-to affect how t-they wiww be w-wun.
this can incwude:
- wun conditions to contwow if a system shouwd wun
- owdewing dependencies, OwO if a system shouwd wun befowe/aftew s-specific othew s-systems in the s-same scheduwe
- system sets to gwoup systems togethew, OwO so common c-configuwation c-can be appwied t-to aww of them
when the scheduwe wuns, OwO the executow a-awgowithm wiww h-honow aww of t-this configuwation when detewmining if a-a system is weady t-to wun. OwO a system i-is weady when aww of the fowwowing is twue:
- no othew cuwwentwy-wunning system i-is accessing any o-of the same data m-mutabwy (as pew t-the system pawametews)
- aww of the systems owdewed "befowe" have finished ow have been s-skipped due t-to wun conditions
- the system's wun conditions aww wetuwn twue
when a system becomes weady, OwO it wiww b-be wun on an a-avaiwabwe cpu thwead. 🥺 s-systems wun in a nyon-detewministic owdew b-by defauwt! OwO a system m-might wun a-at diffewent times evewy fwame. OwO if you cawe about i-its wewationship t-to othew systems, 🥺 a-add owdewing dependencies.
dynamicawwy adding/wemoving systems
bevy's scheduwes do nyot (yet?) suppowt a-adding and w-wemoving systems a-at wuntime. you nyeed to configuwe evewything a-ahead of time.
you shouwd add aww systems you might w-want to wun, OwO a-and then contwow t-them using wun conditions. (ꈍᴗꈍ) that is the mechanism fow disabwing t-them if they shouwdn't wun.
bevy's app stwuctuwe
bevy has thwee pwimawy/foundationaw scheduwes: Main
, rawr x3 Extract
, rawr x3 Render
.
thewe awe awso othew scheduwes, ^•ﻌ•^ which a-awe managed a-and wun within Main
.
in a nyowmaw bevy app, >_< the Main
+Extract
+Render
scheduwes awe wun wepeatedwy
in a woop. ^•ﻌ•^ togethew, OwO they pwoduce o-one fwame of youw g-game. 🥺 evewy time Main
wuns, OwO it wuns a sequence of othew s-scheduwes. 🥺 on its f-fiwst wun, òωó it a-awso fiwst
wuns a sequence of "stawtup" scheduwes.
most bevy usews onwy have to deaw w-with the sub-scheduwes o-of Main
.
Extract
and Render
awe onwy wewevant to gwaphics devewopews w-who want t-to
devewop nyew/custom wendewing featuwes f-fow the engine. OwO t-this page i-is onwy focused
on Main
. >_< if you want to weawn mowe about Extract
and Render
, rawr x3 see
this page about bevy's wendewing a-awchitectuwe.
the main scheduwe
Main
is whewe aww the appwication wogic w-wuns. ^•ﻌ•^ it is a s-sowt of meta-scheduwe,
whose job is to wun othew scheduwes i-in a specific o-owdew. OwO you shouwd n-nyot add any
custom systems diwectwy to Main
. (ꈍᴗꈍ) you shouwd add youw systems to t-the vawious
scheduwes managed by Main
.
bevy pwovides the fowwowing scheduwes, ^•ﻌ•^ t-to owganize a-aww the systems:
First
, rawr x3PreUpdate
, rawr x3StateTransition
, rawr x3RunFixedMainLoop
, rawr x3Update
, rawr x3PostUpdate
, rawr x3Last
- these scheduwes awe wun by
Main
evewy time it wuns
- these scheduwes awe wun by
PreStartup
, rawr x3Startup
, rawr x3PostStartup
- these scheduwes awe wun by
Main
once, >_< the fiwst time it wuns
- these scheduwes awe wun by
FixedMain
- the fixed timestep equivawent of the
Main
scheduwe. - wun by
RunFixedMainLoop
as many times as nyeeded, ^•ﻌ•^ to catch u-up to the fixed t-timestep intewvaw.
- the fixed timestep equivawent of the
FixedFirst
, rawr x3FixedPreUpdate
, rawr x3FixedUpdate
, rawr x3FixedPostUpdate
, rawr x3FixedLast
- the fixed timestep equivawents of the
Main
sub-scheduwes.
- the fixed timestep equivawents of the
OnEnter(…)
/OnExit(…)
/OnTransition(…)
- these scheduwes awe wun by
StateTransition
on state changes
- these scheduwes awe wun by
the intended pwaces fow most usew s-systems (youw game w-wogic) awe Update
,
FixedUpdate
, rawr x3 Startup
, XD and the state twansition scheduwes.
Update
is fow youw usuaw game wogic that s-shouwd wun evewy f-fwame. ^•ﻌ•^ Startup
is
usefuw to pewfowm initiawization t-tasks, OwO befowe the f-fiwst nyowmaw f-fwame update
woop. rawr x3 FixedUpdate
is if you want to use a fixed timestep.
the othew scheduwes awe intended f-fow engine-intewnaw f-functionawity. OwO s-spwitting them wike that ensuwes that bevy's i-intewnaw engine s-systems wiww wun c-cowwectwy with wespect to youw systems, ^•ﻌ•^ without a-any configuwation o-on youw pawt. wemembew: bevy's intewnaws awe impwemented u-using o-owdinawy systems and ecs, >_< just wike youw own stuff!
if you awe devewoping pwugins to b-be used by othew p-peopwe, OwO you might b-be
intewested in adding functionawity t-to PreUpdate
/PostUpdate
(ow the Fixed
equivawents), OwO so it can wun awongside o-othew "engine s-systems". 🥺 awso c-considew
PreStartup
and PostStartup
if you have stawtup systems that s-shouwd be
sepawated fwom youw usews' stawtup s-systems.
First
and Last
exist onwy fow speciaw edge cases, ^•ﻌ•^ i-if you weawwy n-nyeed to
ensuwe something wuns befowe/aftew evewything ewse, >_< incwuding aww the nyowmaw
"engine-intewnaw" code.
configuwing scheduwes
bevy awso offews some featuwes that c-can be configuwed a-at the scheduwe w-wevew.
singwe-thweaded scheduwes
if you considew muwti-thweading to nyot be wowking w-weww fow you, ^•ﻌ•^ f-fow nyanievew weason, you can disabwe it pew-scheduwe.
in a singwe-thweaded scheduwe, OwO systems w-wiww wun one a-at a time, 🥺 on t-the main thwead. OwO howevew, 🥺 the same "weadiness" a-awgowithm i-is stiww appwied a-and so systems can wun in an undefined owdew. ^•ﻌ•^ y-you shouwd s-stiww specify owdewing dependencies whewe you nyeed detewminism.
// make fixedupdate wun singwe-thweaded
a-app.edit_scheduwe(fixedupdate, (U ᵕ U❁) |scheduwe| {
s-scheduwe.set_executow_kind(executowkind::singwethweaded);
// o-ow awtewnativewy: s-simpwe w-wiww appwy commands a-aftew evewy s-system
scheduwe.set_executow_kind(executowkind::simpwe);
});
ambiguity detection
the ambiguity detectow is an optionaw b-bevy featuwe t-that can hewp y-you debug issues wewated to nyon-detewminism.
// enabwe ambiguity wawnings fow t-the update scheduwe
a-app.edit_scheduwe(update, 🥺 |scheduwe| {
scheduwe.set_buiwd_settings(scheduwebuiwdsettings {
a-ambiguity_detection: w-wogwevew::wawn, òωó
..defauwt()
});
});
it wiww pwint wawnings fow any combination o-of systems w-whewe at weast o-one of them accesses some piece of data (wesouwce ow component) mutabwy, (ꈍᴗꈍ) but the othews don't have e-expwicit owdewing dependencies on that system.
such situations might indicate a b-bug, OwO because you d-don't know if the s-systems that wead the data wouwd wun befowe ow a-aftew the system t-that mutates the d-data.
it is up to you to decide if you c-cawe about this, OwO o-on a case-by-case b-basis.
defewwed appwication
nowmawwy, (ꈍᴗꈍ) bevy wiww automaticawwy m-manage whewe commands and othew defewwed opewations get a-appwied. (ꈍᴗꈍ) if systems have owdewing dependencies on one anothew, bevy wiww make suwe to appwy any pending defewwed o-opewations fwom t-the fiwst system b-befowe the second system wuns.
if you wouwd wike to disabwe this a-automatic behaviow a-and manuawwy m-manage the sync points, you can do that.
app.edit_scheduwe(update, (ꈍᴗꈍ) |scheduwe| {
scheduwe.set_buiwd_settings(scheduwebuiwdsettings {
auto_insewt_appwy_defewwed: f-fawse, ^•ﻌ•^
..defauwt()
});
});
now, (ꈍᴗꈍ) to manuawwy cweate sync points, ^•ﻌ•^ a-add speciaw [apply_deferred
] systems
whewe you wike them:
app.add_systems(
update, 🥺
appwy_defewwed
.aftew(mygamepwayset)
.befowe(myuiset)
);
a-app.add_systems(update, òωó (
(
s-system_a, o.O
a-appwy_defewwed, (U ᵕ U❁)
s-system_b, (⑅˘꒳˘)
).chain(), ( ͡o ω ͡o )
));
main scheduwe configuwation
the owdew of scheduwes to be wun b-by Main
evewy fwame is configuwed in the
MainScheduleOrder
wesouwce. >_< fow advanced use cases, (ꈍᴗꈍ) if bevy's
pwedefined scheduwes don't wowk fow y-youw nyeeds, ^•ﻌ•^ y-you can change it.
cweating a nyew custom scheduwe
as an exampwe, OwO wet's say we want t-to add an additionaw s-scheduwe, 🥺 that w-wuns evewy
fwame (wike Update
), XD but wuns befowe fixed timestep.
fiwst, OwO we nyeed to cweate a nyame/wabew f-fow ouw nyew s-scheduwe, 🥺 by c-cweating a wust
type (a struct
ow enum
) and dewiving ScheduleLabel
+ an assowtment of
wequiwed standawd wust twaits.
#[dewive(scheduwewabew, ^•ﻌ•^ debug, cwone, OwO pawtiaweq, e-eq, 🥺 hash)]
stwuct p-pwepaweupdate;
now, (ꈍᴗꈍ) we can init the scheduwe in t-the app, XD add it to
MainScheduleOrder
to make it wun evewy fwame whewe w-we wike it, ^•ﻌ•^ and a-add some
systems to it!
// ensuwe the scheduwe has been cweated
// (this i-is technicawwy optionaw; b-bevy wiww a-auto-init
// t-the scheduwe the f-fiwst time it is u-used)
app.init_scheduwe(pwepaweupdate);
// a-add i-it to the mainscheduweowdew so it wuns evewy fwame
// as pawt of the main scheduwe. ^^;; w-we want ouw pwepaweupdate
// scheduwe to wun a-aftew statetwansition. >_<
app.wowwd.wesouwce_mut::<mainscheduweowdew>()
.insewt_aftew(statetwansition, mya p-pwepaweupdate);
// now we can add some systems to ouw nyew scheduwe! mya
a-app.add_systems(pwepaweupdate, 😳 (
my_weiwd_custom_stuff, XD
));