Trait bevy::ecs::component::Component

pub trait Component: Send + Sync + 'static {
    type Storage: ComponentStorage;
}
Expand description

A data type that can be used to store data for an entity.

Component is a derivable trait: this means that a data type can implement it by applying a #[derive(Component)] attribute to it. However, components must always satisfy the Send + Sync + 'static trait bounds.

Examples

Components can take many forms: they are usually structs, but can also be of every other kind of data type, like enums or zero sized types. The following examples show how components are laid out in code.

// A component can contain data...
#[derive(Component)]
struct LicensePlate(String);

// ... but it can also be a zero-sized marker.
#[derive(Component)]
struct Car;

// Components can also be structs with named fields...
#[derive(Component)]
struct VehiclePerformance {
    acceleration: f32,
    top_speed: f32,
    handling: f32,
}

// ... or enums.
#[derive(Component)]
enum WheelCount {
    Two,
    Three,
    Four,
}

Component and data access

See the entity module level documentation to learn how to add or remove components from an entity.

See the documentation for Query to learn how to access component data from a system.

Choosing a storage type

Components can be stored in the world using different strategies with their own performance implications. By default, components are added to the Table storage, which is optimized for query iteration.

Alternatively, components can be added to the SparseSet storage, which is optimized for component insertion and removal. This is achieved by adding an additional #[component(storage = "SparseSet")] attribute to the derive one:

#[derive(Component)]
#[component(storage = "SparseSet")]
struct ComponentA;

Implementing the trait for foreign types

As a consequence of the orphan rule, it is not possible to separate into two different crates the implementation of Component from the definition of a type. This means that it is not possible to directly have a type defined in a third party library as a component. This important limitation can be easily worked around using the newtype pattern: this makes it possible to locally define and implement Component for a tuple struct that wraps the foreign type. The following example gives a demonstration of this pattern.

// `Component` is defined in the `bevy_ecs` crate.
use bevy_ecs::component::Component;

// `Duration` is defined in the `std` crate.
use std::time::Duration;

// It is not possible to implement `Component` for `Duration` from this position, as they are
// both foreign items, defined in an external crate. However, nothing prevents to define a new
// `Cooldown` type that wraps `Duration`. As `Cooldown` is defined in a local crate, it is
// possible to implement `Component` for it.
#[derive(Component)]
struct Cooldown(Duration);

!Sync Components

A !Sync type cannot implement Component. However, it is possible to wrap a Send but not Sync type in SyncCell or the currently unstable Exclusive to make it Sync. This forces only having mutable access (&mut T only, never &T), but makes it safe to reference across multiple threads.

This will fail to compile since RefCell is !Sync.

#[derive(Component)]
struct NotSync {
   counter: RefCell<usize>,
}

This will compile since the RefCell is wrapped with SyncCell.

use bevy_utils::synccell::SyncCell;

// This will compile.
#[derive(Component)]
struct ActuallySync {
   counter: SyncCell<RefCell<usize>>,
}

Required Associated Types§

Implementors§

§

impl Component for DebandDitherwhere DebandDither: Send + Sync + 'static,

§

impl Component for Tonemappingwhere Tonemapping: Send + Sync + 'static,

§

impl Component for AlphaModewhere AlphaMode: Send + Sync + 'static,

§

impl Component for ClusterConfigwhere ClusterConfig: Send + Sync + 'static,

§

impl Component for LightEntitywhere LightEntity: Send + Sync + 'static,

§

impl Component for Projectionwhere Projection: Send + Sync + 'static,

§

impl Component for Visibilitywhere Visibility: Send + Sync + 'static,

§

impl Component for Anchorwhere Anchor: Send + Sync + 'static,

§

impl Component for FocusPolicywhere FocusPolicy: Send + Sync + 'static,

§

impl Component for Interactionwhere Interaction: Send + Sync + 'static,

§

impl Component for ZIndexwhere ZIndex: Send + Sync + 'static,

§

impl Component for AccessibilityNodewhere AccessibilityNode: Send + Sync + 'static,

§

impl Component for AnimationPlayerwhere AnimationPlayer: Send + Sync + 'static,

§

impl Component for Namewhere Name: Send + Sync + 'static,

§

impl Component for BloomSettingswhere BloomSettings: Send + Sync + 'static,

§

impl Component for Camera2dwhere Camera2d: Send + Sync + 'static,

§

impl Component for Camera3dwhere Camera3d: Send + Sync + 'static,

§

impl Component for CameraFxaaPipelinewhere CameraFxaaPipeline: Send + Sync + 'static,

§

impl Component for Fxaawhere Fxaa: Send + Sync + 'static,

§

impl Component for MsaaWritebackBlitPipelinewhere MsaaWritebackBlitPipeline: Send + Sync + 'static,

§

impl Component for DepthPrepasswhere DepthPrepass: Send + Sync + 'static,

§

impl Component for NormalPrepasswhere NormalPrepass: Send + Sync + 'static,

§

impl Component for ViewPrepassTextureswhere ViewPrepassTextures: Send + Sync + 'static,

§

impl Component for ViewTonemappingPipelinewhere ViewTonemappingPipeline: Send + Sync + 'static,

§

impl Component for ViewUpscalingPipelinewhere ViewUpscalingPipeline: Send + Sync + 'static,

§

impl Component for GltfExtraswhere GltfExtras: Send + Sync + 'static,

§

impl Component for Childrenwhere Children: Send + Sync + 'static,

§

impl Component for Parentwhere Parent: Send + Sync + 'static,

§

impl Component for CascadeShadowConfigwhere CascadeShadowConfig: Send + Sync + 'static,

§

impl Component for Cascadeswhere Cascades: Send + Sync + 'static,

§

impl Component for CascadesVisibleEntitieswhere CascadesVisibleEntities: Send + Sync + 'static,

§

impl Component for Clusterswhere Clusters: Send + Sync + 'static,

§

impl Component for CubemapVisibleEntitieswhere CubemapVisibleEntities: Send + Sync + 'static,

§

impl Component for DirectionalLightwhere DirectionalLight: Send + Sync + 'static,

§

impl Component for EnvironmentMapLightwhere EnvironmentMapLight: Send + Sync + 'static,

§

impl Component for ExtractedClusterConfigwhere ExtractedClusterConfig: Send + Sync + 'static,

§

impl Component for ExtractedClustersPointLightswhere ExtractedClustersPointLights: Send + Sync + 'static,

§

impl Component for ExtractedDirectionalLightwhere ExtractedDirectionalLight: Send + Sync + 'static,

§

impl Component for ExtractedPointLightwhere ExtractedPointLight: Send + Sync + 'static,

§

impl Component for FogSettingswhere FogSettings: Send + Sync + 'static,

§

impl Component for MeshUniformwhere MeshUniform: Send + Sync + 'static,

§

impl Component for MeshViewBindGroupwhere MeshViewBindGroup: Send + Sync + 'static,

§

impl Component for NotShadowCasterwhere NotShadowCaster: Send + Sync + 'static,

§

impl Component for NotShadowReceiverwhere NotShadowReceiver: Send + Sync + 'static,

§

impl Component for PointLightwhere PointLight: Send + Sync + 'static,

§

impl Component for ShadowViewwhere ShadowView: Send + Sync + 'static,

§

impl Component for SkinnedMeshJointswhere SkinnedMeshJoints: Send + Sync + 'static,

§

impl Component for SpotLightwhere SpotLight: Send + Sync + 'static,

§

impl Component for ViewClusterBindingswhere ViewClusterBindings: Send + Sync + 'static,

§

impl Component for ViewFogUniformOffsetwhere ViewFogUniformOffset: Send + Sync + 'static,

§

impl Component for ViewLightEntitieswhere ViewLightEntities: Send + Sync + 'static,

§

impl Component for ViewLightsUniformOffsetwhere ViewLightsUniformOffset: Send + Sync + 'static,

§

impl Component for ViewShadowBindingswhere ViewShadowBindings: Send + Sync + 'static,

§

impl Component for VisiblePointLightswhere VisiblePointLights: Send + Sync + 'static,

§

impl Component for Wireframewhere Wireframe: Send + Sync + 'static,

§

impl Component for Camerawhere Camera: Send + Sync + 'static,

§

impl Component for CameraRenderGraphwhere CameraRenderGraph: Send + Sync + 'static,

§

impl Component for ExtractedCamerawhere ExtractedCamera: Send + Sync + 'static,

§

impl Component for OrthographicProjectionwhere OrthographicProjection: Send + Sync + 'static,

§

impl Component for PerspectiveProjectionwhere PerspectiveProjection: Send + Sync + 'static,

§

impl Component for SkinnedMeshwhere SkinnedMesh: Send + Sync + 'static,

§

impl Component for Aabbwhere Aabb: Send + Sync + 'static,

§

impl Component for CascadesFrustawhere CascadesFrusta: Send + Sync + 'static,

§

impl Component for CubemapFrustawhere CubemapFrusta: Send + Sync + 'static,

§

impl Component for Frustumwhere Frustum: Send + Sync + 'static,

§

impl Component for ColorGradingwhere ColorGrading: Send + Sync + 'static,

§

impl Component for ComputedVisibilitywhere ComputedVisibility: Send + Sync + 'static,

§

impl Component for ExtractedViewwhere ExtractedView: Send + Sync + 'static,

§

impl Component for NoFrustumCullingwhere NoFrustumCulling: Send + Sync + 'static,

§

impl Component for RenderLayerswhere RenderLayers: Send + Sync + 'static,

§

impl Component for ViewDepthTexturewhere ViewDepthTexture: Send + Sync + 'static,

§

impl Component for ViewTargetwhere ViewTarget: Send + Sync + 'static,

§

impl Component for ViewUniformOffsetwhere ViewUniformOffset: Send + Sync + 'static,

§

impl Component for VisibleEntitieswhere VisibleEntities: Send + Sync + 'static,

§

impl Component for SceneInstancewhere SceneInstance: Send + Sync + 'static,

§

impl Component for ExtractedSpritewhere ExtractedSprite: Send + Sync + 'static,

§

impl Component for Mesh2dHandlewhere Mesh2dHandle: Send + Sync + 'static,

§

impl Component for Mesh2dUniformwhere Mesh2dUniform: Send + Sync + 'static,

§

impl Component for Mesh2dViewBindGroupwhere Mesh2dViewBindGroup: Send + Sync + 'static,

§

impl Component for Spritewhere Sprite: Send + Sync + 'static,

§

impl Component for SpriteBatchwhere SpriteBatch: Send + Sync + 'static,

§

impl Component for TextureAtlasSpritewhere TextureAtlasSprite: Send + Sync + 'static,

§

impl Component for Text2dBoundswhere Text2dBounds: Send + Sync + 'static,

§

impl Component for Textwhere Text: Send + Sync + 'static,

§

impl Component for TextLayoutInfowhere TextLayoutInfo: Send + Sync + 'static,

§

impl Component for GlobalTransformwhere GlobalTransform: Send + Sync + 'static,

§

impl Component for Transformwhere Transform: Send + Sync + 'static,

§

impl Component for UiCameraConfigwhere UiCameraConfig: Send + Sync + 'static,

§

impl Component for BackgroundColorwhere BackgroundColor: Send + Sync + 'static,

§

impl Component for CalculatedClipwhere CalculatedClip: Send + Sync + 'static,

§

impl Component for CalculatedSizewhere CalculatedSize: Send + Sync + 'static,

§

impl Component for DefaultCameraViewwhere DefaultCameraView: Send + Sync + 'static,

§

impl Component for Nodewhere Node: Send + Sync + 'static,

§

impl Component for RelativeCursorPositionwhere RelativeCursorPosition: Send + Sync + 'static,

§

impl Component for Stylewhere Style: Send + Sync + 'static,

§

impl Component for UiBatchwhere UiBatch: Send + Sync + 'static,

§

impl Component for UiImagewhere UiImage: Send + Sync + 'static,

§

impl Component for Buttonwhere Button: Send + Sync + 'static,

§

impl Component for Labelwhere Label: Send + Sync + 'static,

§

impl Component for PrimaryWindowwhere PrimaryWindow: Send + Sync + 'static,

§

impl Component for RawHandleWrapperwhere RawHandleWrapper: Send + Sync + 'static,

§

impl Component for Windowwhere Window: Send + Sync + 'static,

§

impl<C> Component for DynamicUniformIndex<C>where C: Component, DynamicUniformIndex<C>: Send + Sync + 'static,

§

impl<I> Component for RenderPhase<I>where I: PhaseItem, RenderPhase<I>: Send + Sync + 'static,

§

impl<T> Component for Handle<T>where T: Asset, Handle<T>: Send + Sync + 'static,