Expand description
§ECS
ReactCommands
makes it easy to register and unregister ECS hooks. Reactors are most useful when you need to pass information (e.g. entity IDs) into a reaction system.
A reactor will run in the first apply_deferred
after its reaction trigger is detected. If a reactor triggers other reactors, they will run immediately after the initial reactor (until the entire tree of reactions terminates). Recursive reactions are currently not supported.
§Registering Reactors
Reactors are registered with ReactCommands
. You must specify a ‘reaction trigger’:
fn setup(mut rcommands: ReactCommands)
{
rcommands.on(resource_mutation::<A>(),
|a: ReactRes<A>|
{
//...
}
);
}
The available reaction triggers are:
resource_mutation<R: ReactResource>()
insertion<C: ReactComponent>()
mutation<C: ReactComponent>()
removal<C: ReactComponent>()
- [
entity_insertion<C: ReactComponent>(entity)
] - [
entity_mutation<C: ReactComponent>(entity)
] - [
entity_removal<C: ReactComponent>(entity)
] event<E>()
A reactor can be associated with multiple reaction triggers:
fn setup(mut rcommands: ReactCommands)
{
rcommands.on((resource_mutation::<A>(), entity_insertion<B>(entity)),
move |a: ReactRes<A>, q: Query<&B>|
{
q.get(entity);
//...etc.
}
);
}
§Revoking Reactors
Reactors can be revoked with RevokeToken
s obtained on registration.
let token = rcommands.on(resource_mutation::<A>(), || { todo!(); });
rcommands.revoke(token);
§Trigger Type: Resource Mutation
Add a reactive resource to your app:
#[derive(ReactResource)]
struct Counter(u32);
app.add_plugins(ReactPlugin)
.add_react_resource(Counter);
Mutate the resource:
fn increment(mut rcommands: ReactCommands, mut counter: ReactResMut<Counter>)
{
counter.get_mut(&mut rcommands).0 += 1;
}
React to the resource mutation:
fn setup(mut rcommands: ReactCommands)
{
rcommands.on(resource_mutation::<Counter>(),
|counter: ReactRes<Counter>|
{
println!("count: {}", counter.0);
}
);
}
§Trigger Type: Component Insertion/Mutation/Removal
#[derive(ReactComponent)]
struct Health(u16);
fn setup(mut rcommands: ReactCommands)
{
let entity = rcommands.commands().spawn_empty().id();
rcommands.insert(entity, Health(0u16));
rcommands.on(entity_mutation::<Health>(entity)
move |q: Query<&React<Health>>|
{
let health = q.get(entity).unwrap();
println!("health: {}", health.0);
}
);
}
fn add_health(mut rcommands: ReactCommands, mut q: Query<&mut React<Health>>)
{
for health in q.iter_mut()
{
health.get_mut(&mut rcommands).0 += 10;
}
}
Entity-agnostic triggers (insertion<C>()
, mutation<C>()
, removal<C>()
) can only be grouped with each other, since their reactor requires an In<Entity>
system parameter:
#[derive(ReactComponent)]
struct A;
#[derive(ReactComponent)]
struct B;
rcommands.on((insertion::<A>(), removal::<B>()),
|In(entity): In<Entity>, a: Query<(Option<&React<A>>, Option<&React<B>>)>|
{
//...
}
);
§Trigger Type: Events
Register a react event:
app.add_react_event::<u32>();
Send an event:
rcommands.send(0u32);
React to the event, using the ReactEventReader
to access event data:
rcommands.on(event::<u32>(),
|mut events: ReactEventReader<u32>|
{
for event in events.iter()
{
println!("react u32: {}", event);
}
}
);
§Trigger Type: Despawns
React to despawns with the ReactCommands::on_despawn()
method:
rcommands.on_despawn(entity, move || println!("entity despawned: {}", entity));
§One-off Reactors
If you only want a reactor to run once, use ReactCommands::once()
:
let entity = rcommands.commands().spawn(Player);
rcommands.once(event::<ResetEverything>(),
move |world: &mut World|
{
world.despawn(entity);
}
);
Structs§
- Auto
Despawn Signal - RAII handle to a despawn signal.
- Auto
Despawner - Creates
AutoDespawnSignal
s. - CallId
- An identifier for CallableSystems. Each identifier represents a unique system context.
- Call
Once - Callback wrapper for FnOnce functions. Implements
Command
. - Call
Once With - Callback wrapper that lets you call with a value once. The helper returned by
.call_with()
implementsCommand
. - Callback
- Callback wrapper. Implements
Command
. - Callback
With - Callback wrapper that lets you call with a value. The helper returned by
.call_with()
implementsCommand
. - Callwith
- Callback wrapper with a specific call value baked in. Implements
Command
. - Callwith
Once - Callback wrapper with a specific call value baked in. Implements
Command
. - Entity
Insertion - Reaction trigger for
ReactComponent
insertions on a specific entity. - Entity
Mutation - Reaction trigger for
ReactComponent
mutations on a specific entity. - Entity
Removal - Reaction trigger for
ReactComponent
removals from a specific entity. - Event
- Reaction trigger for events.
- IdMapped
Systems - Tracks named systems.
- Insertion
- Reaction trigger for
ReactComponent
insertions on any entity. - Mutation
- Reaction trigger for
ReactComponent
mutations on any entity. - React
- Component wrapper that enables reacting to component mutations.
- React
Commands - Drives reactivity.
- React
Event Reader - Provides access to react events of type
E
. - React
Plugin - React
Res - Immutable reader for reactive resources.
- React
ResMut - Mutable wrapper for reactive resources.
- Removal
- Reaction trigger for
ReactComponent
removals from any entity. - Resource
Mutation - Reaction trigger for
ReactResource
mutations. - Revoke
Token - Token for revoking reactors.
- SysCall
- Callback wrapper that mimics
syscall
(without an implicit call toapply_deferred
). - SysId
- System identifier for referencing spawned systems.
- SysName
- System identifier for use in named systems.
Enums§
- Callback
System - Represents a system callback.
- Reactor
Type
Traits§
- Auto
Despawn AppExt - Extends the
App
API with a method to set up auto despawning. - Basic
Callable System - Implemented types can be called like normal functions.
- Callable
System - Implemented types can be called like normal functions.
- React
Component - Tag trait for reactive components.
- React
Event AppExt - Extends the
App
API with reactive event methods. - React
ResApp Ext - React
ResCommands Ext - React
ResWorld Ext - Extends the
World
API with reactive resource methods. - React
Resource - Tag trait for reactive resources.
- Reaction
Trigger - Helper trait for registering reactors with
ReactCommands
. - Reaction
Trigger Bundle - Helper trait for registering reactors with
ReactCommands
. - System
Caller Commands Ext
Functions§
- call
- Call a callable system (one function argument).
- call_
basic - Call a callable system (no function arguments).
- entity_
insertion - Obtain a
EntityInsertion
reaction trigger. - entity_
mutation - Obtain a
EntityMutation
reaction trigger. - entity_
removal - Obtain a
EntityRemoval
reaction trigger. - event
- Obtain an [
Event
] reaction trigger. - insertion
- Obtain a
Insertion
reaction trigger. - mutation
- Obtain a
Mutation
reaction trigger. - named_
syscall - Execute a named system on some data then apply the system’s deferred commands.
- named_
syscall_ direct - Directly invoke a named system.
- prep_
fncall - Wrap a
Fn
system in a system that consumes the system input. - react_
to_ all_ removals_ and_ despawns - Iteratively react to component removals and entity despawns until all reaction chains have ended.
- react_
to_ despawns - React to tracked despawns.
- react_
to_ removals - React to component removals.
- reactor_
registration - register_
named_ system - Register a named system for future use.
- register_
named_ system_ from - removal
- Obtain a
Removal
reaction trigger. - resource_
mutation - Obtain a
ResourceMutation
reaction trigger. - spawn_
rc_ system - Spawn a ref-counted system.
- spawn_
rc_ system_ from - Spawn a ref-counted system.
- spawn_
system - Spawn a system as an entity.
- spawn_
system_ from - Spawn a system as an entity.
- spawned_
syscall - Execute a pre-spawned system on some data then apply the system’s deferred commands.
- syscall
- Execute a system on some data then apply the system’s deferred commands.
- try_
add_ component_ to_ entity - Try to add a component to an entity.
- try_
callback - Try to invoke the callback
C
onentity
. - try_
callback_ with - Try to invoke the callback
C
onentity
withvalue
. - try_
remove_ component_ from_ entity - Try to remove a component from an entity.
- try_
set_ component - Try to set the value of a component on an entity.
- try_
update_ component_ if_ different - Try to update the value of a component on an entity.