Expand description
§bevy_expected_components
Runtime validation for Bevy component dependencies. Like #[require] but validates
instead of auto-inserting.
§Why Use This?
Bevy’s #[require(T)] automatically inserts missing components using Default.
This doesn’t work when:
- Required components need world access to construct
- You want bugs to surface immediately rather than silently using defaults
- The required component has no sensible default
#[expects(T)] solves this by panicking if expected components are missing at insert
time, making bugs immediately visible during development.
§Performance Warning
This crate adds runtime overhead: every time a component with #[expects(...)] is
inserted, the plugin checks that all expected components exist on the entity.
Recommended usage: Enable only in development and test builds.
// Only add the plugin in debug builds
#[cfg(debug_assertions)]
app.add_plugins(ExpectedComponentsPlugin);§Example
use bevy::prelude::*;
use bevy_expected_components::prelude::*;
#[derive(Component, Default)]
struct Transform;
#[derive(Component, Default)]
struct Velocity;
// PhysicsBody expects Transform and Velocity to exist when it's inserted
#[derive(Component, ExpectComponents)]
#[expects(Transform, Velocity)]
struct PhysicsBody;
fn main() {
let mut app = App::new();
// Enable validation (only in debug builds recommended)
#[cfg(debug_assertions)]
app.add_plugins(ExpectedComponentsPlugin);
// This works - all expected components present
app.world_mut().spawn((PhysicsBody, Transform, Velocity));
// This panics - Velocity is missing!
// app.world_mut().spawn((PhysicsBody, Transform));
}§How It Works
#[derive(ExpectComponents)]generates anExpectComponentstrait implementation- The derive macro registers the type with
inventoryat compile time ExpectedComponentsPluginiterates all registered types and installson_addhooks- When a component is inserted, the hook validates expected components exist
§Comparison with #[require]
| Feature | #[require] | #[expects] |
|---|---|---|
| Missing component | Auto-inserted with Default | Panics |
Requires Default | Yes | No |
| Runtime cost | Archetype lookup | Component existence check |
| Use case | Convenience bundles | Bug detection |
§Limitations
Validates insertion only, not removal. If you later remove an expected component from an entity, no error occurs. This keeps the implementation simple and covers the main use case: catching mistakes at spawn time.
§Future of This Crate
This crate may become unnecessary when Bevy adds native support for non-defaultable required components. Relevant discussions:
- Issue #16194: Require components that can’t be defaulted
- Issue #18717: Support required components which have no sensible default
- Archetype invariants (future Bevy feature)
Modules§
- prelude
- Prelude module for convenient imports.
Structs§
- Expect
Registration - Registration entry for a component with expectations.
- Expected
Components Plugin - Plugin that enables runtime validation of component expectations.
Traits§
- Expect
Components - Trait implemented by components that expect other components to be present.
Derive Macros§
- Expect
Components - Derive macro for generating
ExpectComponentsimplementation.