Expand description
Tools for controlling behavior in an ECS application.
Systems define how an ECS based application behaves.
Systems are added to a Schedule, which is then run.
A system is usually written as a normal function, which is automatically converted into a system.
System functions can have parameters, through which one can query and mutate Bevy ECS state.
Only types that implement SystemParam can be used, automatically fetching data from
the World.
System functions often look like this:
fn update_score_system(
mut query: Query<(&Player, &mut Score)>,
mut round: ResMut<Round>,
) {
for (player, mut score) in &mut query {
if player.alive {
score.0 += round.0;
}
}
round.0 += 1;
}§System ordering
By default, the execution of systems is parallel and not deterministic. Not all systems can run together: if a system mutably accesses data, no other system that reads or writes that data can be run at the same time. These systems are said to be incompatible.
The relative order in which incompatible systems are run matters. When this is not specified, a system order ambiguity exists in your schedule. You can explicitly order systems:
- by calling the
.before(this_system)or.after(that_system)methods when adding them to your schedule - by adding them to a
SystemSet, and then using.configure_sets(ThisSet.before(ThatSet))syntax to configure many systems at once - through the use of
.add_systems((system_a, system_b, system_c).chain())
§Example
// Configure these systems to run in order using `chain()`.
schedule.add_systems((print_first, print_last).chain());
// Prints "HelloWorld!"
schedule.run(&mut world);
// Configure this system to run in between the other two systems
// using explicit dependencies.
schedule.add_systems(print_mid.after(print_first).before(print_last));
// Prints "Hello, World!"
schedule.run(&mut world);
fn print_first() {
print!("Hello");
}
fn print_mid() {
print!(", ");
}
fn print_last() {
println!("World!");
}§System return type
Systems added to a schedule through add_systems may either return
empty () or a Result. Other contexts (like one shot systems) allow
systems to return arbitrary values.
§System parameter list
Following is the complete list of accepted types as system parameters:
QueryResandOption<Res>ResMutandOption<ResMut>CommandsLocalMessageReaderMessageWriterNonSendandOption<NonSend>NonSendMutandOption<NonSendMut>RemovedComponentsSystemNameSystemChangeTickArchetypes(Provides Archetype metadata)Bundles(Provides Bundles metadata)Components(Provides Components metadata)Entities(Provides Entities metadata)- All tuples between 1 to 16 elements where each element implements
SystemParam ParamSet()(unit primitive type)
In addition, the following parameters can be used when constructing a dynamic system with SystemParamBuilder,
but will only provide an empty value when used with an ordinary system:
FilteredResourcesFilteredResourcesMutDynSystemParamVec<P>whereP: SystemParamParamSet<Vec<P>>whereP: SystemParam
Modules§
- command
- Contains the definition of the
Commandtrait, as well as the blanket implementation of the trait for closures. - entity_
command - Contains the definition of the
EntityCommandtrait, as well as the blanket implementation of the trait for closures. - lifetimeless
- Contains type aliases for built-in
SystemParams with'staticlifetimes. This makes it more convenient to refer to these types in contexts where explicit lifetime annotations are required.
Structs§
- Adapter
System - A
Systemthat takes the output ofSand transforms it by applyingFuncto it. - Cached
System Id - A cached
SystemIddistinguished by the unique function type of its system. - Combinator
System - A
Systemdefined by combining two other systems. The behavior of this combinator is specified by implementing theCombinetrait. For a full usage example, see the docs forCombine. - Commands
- A
Commandqueue to perform structural changes to theWorld. - Deferred
- A
SystemParamthat stores a buffer which gets applied to theWorldduringApplyDeferred. This is used internally byCommandsto deferWorldmutations. - DynParam
Builder - A
SystemParamBuilderfor aDynSystemParam. See theDynSystemParamdocs for examples. - DynSystem
Param - A
SystemParamwith a type that can be configured at runtime. - DynSystem
Param State - The
SystemParam::Statefor aDynSystemParam. - Entity
Commands - A list of commands that will be run to modify an
Entity. - Entity
Entry Commands - A wrapper around
EntityCommandswith convenience methods for working with a specified component type. - Exclusive
Function System - A function system that runs with exclusive
Worldaccess. - Filtered
Resources MutParam Builder - A
SystemParamBuilderfor aFilteredResourcesMut. See theFilteredResourcesMutdocs for examples. - Filtered
Resources Param Builder - A
SystemParamBuilderfor aFilteredResources. See theFilteredResourcesdocs for examples. - Function
System - The
Systemcounter part of an ordinary function. - If
- A
SystemParamthat wraps another parameter and causes its system to skip instead of failing when the parameter is invalid. - IfBuilder
- A
SystemParamBuilderfor aIf. - In
- A
SystemInputtype which denotes that aSystemreceives an input value of typeTfrom its caller. - InMut
- A
SystemInputtype which denotes that aSystemreceives a mutable reference to a value of typeTfrom its caller. - InRef
- A
SystemInputtype which denotes that aSystemreceives a read-only reference to a value of typeTfrom its caller. - Into
Adapter System - An
IntoSystemcreating an instance ofAdapterSystem. - Into
Pipe System - An
IntoSystemcreating an instance ofPipeSystem. - Local
- A system local
SystemParam. - Local
Builder - A
SystemParamBuilderfor aLocal. The provided value will be used as the initial value of theLocal. - NonSend
- Shared borrow of a non-
Sendresource. - NonSend
Marker - A dummy type that is
!Send, to force systems to run on the main thread. - NonSend
Mut - Unique borrow of a non-
Sendresource. - Option
Builder - A
SystemParamBuilderfor anOption. - Parallel
Commands - An alternative to
Commandsthat can be used in parallel contexts, such as those inQuery::par_iter. - Param
Builder - A
SystemParamBuilderfor anySystemParamthat uses its default initialization. - Param
Set - A collection of potentially conflicting
SystemParams allowed by disjoint access. - Param
SetBuilder - A
SystemParamBuilderfor aParamSet. - Pipe
System - A
Systemcreated by piping the output of the first system into the input of the second. - Populated
- System parameter that works very much like
Queryexcept it always contains at least one matching entity. - Query
- A system parameter that provides selective access to the
Componentdata stored in aWorld. - Query
Lens - Type returned from
Query::transmute_lenscontaining the newQueryState. - Query
Param Builder - A
SystemParamBuilderfor aQuery. This takes a closure accepting an&mutQueryBuilderand uses the builder to construct the query’s state. This can be used to add additional filters, or to configure the components available toFilteredEntityReforFilteredEntityMut. - Removed
System - A system that has been removed from the registry. It contains the system and whether or not it has been initialized.
- Res
- Shared borrow of a
Resource. - ResMut
- Unique mutable borrow of a
Resource. - Result
Builder - A
SystemParamBuilderfor aResultofSystemParamValidationError. - Single
- System parameter that provides access to single entity’s components, much like
Query::single/Query::single_mut. - Static
System Input - A helper for using
SystemInputs in generic contexts. - Static
System Param - A helper for using system parameters in generic contexts
- System
Change Tick - A
SystemParamthat reads the previous and current change ticks of the system. - System
Id - An identifier for a registered system.
- System
IdMarker - Marker
Componentfor identifyingSystemIdEntitys. - System
Meta - The metadata of a
System. - System
Name SystemParamthat returns the name of the system which it is used in.- System
Param Validation Error - An error that occurs when a system parameter is not valid, used by system executors to determine what to do with a system.
- System
State - Holds on to persistent state required to drive
SystemParamfor aSystem. - System
State Flags - Bitflags representing system states and requirements.
- With
Input From Wrapper - Constructed in
IntoSystem::with_input_from. - With
Input Wrapper - See
IntoSystem::with_inputfor details.
Enums§
- Registered
System Error - An operation with stored systems failed.
- RunSystem
Error - Running system failed.
Traits§
- Adapt
- Customizes the behavior of an
AdapterSystem - Combine
- Customizes the behavior of a
CombinatorSystem. - Command
- A
Worldmutation. - Entity
Command - A command which gets executed for a given
Entity. - Exclusive
System Param - A parameter that can be used in an exclusive system (a system with an
&mut Worldparameter). Any parameters implementing this trait must come after the&mut Worldparameter. - Exclusive
System Param Function - A trait implemented for all exclusive system functions that can be used as
Systems. - Into
Observer System - Implemented for systems that convert into
ObserverSystem. - Into
Result - A type that may be converted to the output of a
System. This is used to allow systems to return either a plain value or aResult. - Into
System - Conversion trait to turn something into a
System. - Observer
System - Implemented for
Systems that haveOnas the first argument. - Read
Only System Systemtypes that do not modify theWorldwhen run. This is implemented for any systems whose parameters all implementReadOnlySystemParam.- Read
Only System Param - A
SystemParamthat only reads a givenWorld. - RunSystem
Once - Trait used to run a system immediately on a
World. - System
- An ECS system that can be added to a
Schedule - System
Buffer - Types that can be used with
Deferred<T>in systems. This allows storing system-local data which is used to deferWorldmutations. - System
Input - Trait for types that can be used as input to
Systems. - System
Param - A parameter that can be used in a
System. - System
Param Builder - A builder that can create a
SystemParam. - System
Param Function - A trait implemented for all functions that can be used as
Systems.
Functions§
- assert_
is_ read_ only_ system - Ensure that a given function is a read-only system.
- assert_
is_ system - Ensure that a given function is a system.
- assert_
system_ does_ not_ conflict - Ensures that the provided system doesn’t conflict with itself.
Type Aliases§
- Boxed
Read Only System - A convenience type alias for a boxed
ReadOnlySystemtrait object. - Boxed
System - A convenience type alias for a boxed
Systemtrait object. - Exclusive
System Param Item - Shorthand way of accessing the associated type
ExclusiveSystemParam::Itemfor a givenExclusiveSystemParam. - Schedule
System - Type alias for a
BoxedSystemthat aSchedulecan store. - System
In - Shorthand way to get the
System::Infor aSystemas aSystemInput::Inner. - System
Param Item - Shorthand way of accessing the associated type
SystemParam::Itemfor a givenSystemParam.
Derive Macros§
- System
Param - Implement
SystemParamto use a struct as a parameter in a system