Trait bevy::ecs::component::Component

source ·
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§

source

type Storage: ComponentStorage

A marker type indicating the storage type used for this component. This must be either TableStorage or SparseStorage.

Implementors§

source§

impl Component for DependencyLoadState
where DependencyLoadState: Send + Sync + 'static,

source§

impl Component for LoadState
where LoadState: Send + Sync + 'static,

source§

impl Component for RecursiveDependencyLoadState

source§

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

source§

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

source§

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

source§

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

source§

impl Component for ShadowFilteringMethod
where ShadowFilteringMethod: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

impl Component for ImageScaleMode
where ImageScaleMode: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl Component for AudioSink
where AudioSink: Send + Sync + 'static,

source§

impl Component for PlaybackSettings
where PlaybackSettings: Send + Sync + 'static,

source§

impl Component for SpatialAudioSink
where SpatialAudioSink: Send + Sync + 'static,

source§

impl Component for SpatialListener
where SpatialListener: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for ContrastAdaptiveSharpeningSettings

source§

impl Component for DenoiseCAS
where DenoiseCAS: Send + Sync + 'static,

source§

impl Component for ViewCASPipeline
where ViewCASPipeline: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for ViewTransmissionTexture
where ViewTransmissionTexture: Send + Sync + 'static,

source§

impl Component for DeferredLightingIdDepthTexture

source§

impl Component for TemporalAntiAliasSettings
where TemporalAntiAliasSettings: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

impl Component for DeferredPrepass
where DeferredPrepass: Send + Sync + 'static,

source§

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

source§

impl Component for MotionVectorPrepass
where MotionVectorPrepass: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for Skybox
where Skybox: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for ShowAabbGizmo
where ShowAabbGizmo: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

impl Component for DeferredLightingPipeline
where DeferredLightingPipeline: Send + Sync + 'static,

source§

impl Component for PbrDeferredLightingDepthId

source§

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

source§

impl Component for IrradianceVolume
where IrradianceVolume: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl Component for ExtractedClustersPointLights

source§

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

source§

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

source§

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

source§

impl Component for LightProbe
where LightProbe: Send + Sync + 'static,

source§

impl Component for Lightmap
where Lightmap: Send + Sync + 'static,

source§

impl Component for MaterialBindGroupId
where MaterialBindGroupId: Send + Sync + 'static,

source§

impl Component for MeshTransforms
where MeshTransforms: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

impl Component for PreviousGlobalTransform
where PreviousGlobalTransform: Send + Sync + 'static,

source§

impl Component for PreviousViewProjection
where PreviousViewProjection: Send + Sync + 'static,

source§

impl Component for PreviousViewProjectionUniformOffset

source§

impl Component for ScreenSpaceAmbientOcclusionSettings

source§

impl Component for ScreenSpaceAmbientOcclusionTextures

source§

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

source§

impl Component for SkinIndex
where SkinIndex: Send + Sync + 'static,

source§

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

source§

impl Component for TransmittedShadowReceiver
where TransmittedShadowReceiver: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

impl Component for ViewLightProbesUniformOffset

source§

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

source§

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

source§

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

source§

impl Component for NoWireframe
where NoWireframe: Send + Sync + 'static,

source§

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

source§

impl Component for WireframeColor
where WireframeColor: Send + Sync + 'static,

source§

impl Component for NoAutomaticBatching
where NoAutomaticBatching: Send + Sync + 'static,

source§

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

source§

impl Component for CameraMainTextureUsages
where CameraMainTextureUsages: Send + Sync + 'static,

source§

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

source§

impl Component for Exposure
where Exposure: Send + Sync + 'static,

source§

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

source§

impl Component for ManualTextureView
where ManualTextureView: Send + Sync + 'static,

source§

impl Component for ManualTextureViewHandle
where ManualTextureViewHandle: Send + Sync + 'static,

source§

impl Component for MipBias
where MipBias: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for TemporalJitter
where TemporalJitter: Send + Sync + 'static,

source§

impl Component for MeshMorphWeights
where MeshMorphWeights: Send + Sync + 'static,

source§

impl Component for MorphWeights
where MorphWeights: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl Component for InheritedVisibility
where InheritedVisibility: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl Component for ViewVisibility
where ViewVisibility: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for Material2dBindGroupId
where Material2dBindGroupId: Send + Sync + 'static,

source§

impl Component for Mesh2d
where Mesh2d: Send + Sync + 'static,

source§

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

source§

impl Component for Mesh2dTransforms
where Mesh2dTransforms: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

impl Component for TextureAtlas
where TextureAtlas: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl Component for BorderColor
where BorderColor: Send + Sync + 'static,

source§

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

source§

impl Component for ContentSize
where ContentSize: Send + Sync + 'static,

source§

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

source§

impl Component for IsDefaultUiCamera
where IsDefaultUiCamera: Send + Sync + 'static,

source§

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

source§

impl Component for Outline
where Outline: Send + Sync + 'static,

source§

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

source§

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

source§

impl Component for TargetCamera
where TargetCamera: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

impl Component for TextFlags
where TextFlags: Send + Sync + 'static,

source§

impl Component for UiImageSize
where UiImageSize: Send + Sync + 'static,

source§

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

source§

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

source§

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

source§

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

source§

impl<C> Component for RenderViewLightProbes<C>

source§

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

source§

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

source§

impl<M> Component for UiMaterialBatch<M>
where M: UiMaterial, UiMaterialBatch<M>: Send + Sync + 'static,

source§

impl<T> Component for GpuArrayBufferIndex<T>
where T: GpuArrayBufferable, GpuArrayBufferIndex<T>: Send + Sync + 'static,