Trait bevy_ecs::component::Component

source ·
pub trait Component: Send + Sync + 'static {
    const STORAGE_TYPE: StorageType;

    // Provided method
    fn register_component_hooks(_hooks: &mut ComponentHooks) { ... }
}
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 Constants§

source

const STORAGE_TYPE: StorageType

A constant indicating the storage type used for this component.

Provided Methods§

source

fn register_component_hooks(_hooks: &mut ComponentHooks)

Called when registering this component, allowing mutable access to its ComponentHooks.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl Component for ObserverState

source§

const STORAGE_TYPE: StorageType = StorageType::SparseSet

source§

impl Component for RemovedComponentEntity
where Self: Send + Sync + 'static,

source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::SparseSet

source§

impl Component for OnAdd
where Self: Send + Sync + 'static,

source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::SparseSet

source§

impl Component for OnInsert
where Self: Send + Sync + 'static,

source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::SparseSet

source§

impl Component for OnRemove
where Self: Send + Sync + 'static,

source§

const STORAGE_TYPE: StorageType = bevy_ecs::component::StorageType::SparseSet

source§

impl<E: Event, B: Bundle> Component for Observer<E, B>

source§

const STORAGE_TYPE: StorageType = StorageType::SparseSet

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

impl Component for ActionRequest
where Self: Send + Sync + 'static,

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

impl Component for AnimationTarget
where Self: Send + Sync + 'static,

impl Component for AppExit
where Self: Send + Sync + 'static,

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

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

impl Component for RecursiveDependencyLoadState
where Self: Send + Sync + 'static,

impl Component for UntypedAssetLoadFailedEvent
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

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

impl Component for DepthOfFieldMode
where Self: Send + Sync + 'static,

impl Component for DepthOfFieldPipelines
where Self: Send + Sync + 'static,

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

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

impl Component for AutoExposureSettings
where Self: Send + Sync + 'static,

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

impl Component for ContrastAdaptiveSharpeningSettings
where Self: Send + Sync + 'static,

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

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

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

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

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

impl Component for DeferredLightingIdDepthTexture
where Self: Send + Sync + 'static,

impl Component for AuxiliaryDepthOfFieldTexture
where Self: Send + Sync + 'static,

impl Component for DepthOfFieldSettings
where Self: Send + Sync + 'static,

impl Component for DepthOfFieldUniform
where Self: Send + Sync + 'static,

impl Component for ViewDepthOfFieldBindGroupLayouts
where Self: Send + Sync + 'static,

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

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

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

impl Component for MotionBlurPipelineId
where Self: Send + Sync + 'static,

impl Component for MotionBlur
where Self: Send + Sync + 'static,

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

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

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

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

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

impl Component for PreviousViewData
where Self: Send + Sync + 'static,

impl Component for PreviousViewUniformOffset
where Self: Send + Sync + 'static,

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

impl Component for SmaaBindGroups
where Self: Send + Sync + 'static,

impl Component for SmaaInfoUniformOffset
where Self: Send + Sync + 'static,

impl Component for SmaaSettings
where Self: Send + Sync + 'static,

impl Component for SmaaTextures
where Self: Send + Sync + 'static,

impl Component for ViewSmaaPipelines
where Self: Send + Sync + 'static,

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

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

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

impl Component for EguiContext
where Self: Send + Sync + 'static,

impl Component for EguiInput
where Self: Send + Sync + 'static,

impl Component for EguiOutput
where Self: Send + Sync + 'static,

impl Component for EguiRenderOutput
where Self: Send + Sync + 'static,

impl Component for WindowSize
where Self: Send + Sync + 'static,

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

impl Component for ShowLightGizmo
where Self: Send + Sync + 'static,

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

impl Component for GltfMaterialExtras
where Self: Send + Sync + 'static,

impl Component for GltfMeshExtras
where Self: Send + Sync + 'static,

impl Component for GltfSceneExtras
where Self: Send + Sync + 'static,

impl Component for HierarchyEvent
where Self: Send + Sync + 'static,

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

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

impl Component for GamepadEvent
where Self: Send + Sync + 'static,

impl Component for GamepadRumbleRequest
where Self: Send + Sync + 'static,

impl Component for GamepadAxisChangedEvent
where Self: Send + Sync + 'static,

impl Component for GamepadButtonChangedEvent
where Self: Send + Sync + 'static,

impl Component for GamepadButtonInput
where Self: Send + Sync + 'static,

impl Component for GamepadConnectionEvent
where Self: Send + Sync + 'static,

impl Component for DoubleTapGesture
where Self: Send + Sync + 'static,

impl Component for PanGesture
where Self: Send + Sync + 'static,

impl Component for PinchGesture
where Self: Send + Sync + 'static,

impl Component for RotationGesture
where Self: Send + Sync + 'static,

impl Component for KeyboardFocusLost
where Self: Send + Sync + 'static,

impl Component for KeyboardInput
where Self: Send + Sync + 'static,

impl Component for MouseButtonInput
where Self: Send + Sync + 'static,

impl Component for MouseMotion
where Self: Send + Sync + 'static,

impl Component for MouseWheel
where Self: Send + Sync + 'static,

impl Component for TouchInput
where Self: Send + Sync + 'static,

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

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

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

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

impl Component for PbrDeferredLightingDepthId
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

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

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

impl Component for ExtractedClusterableObjects
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

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

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

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

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

impl Component for PreprocessBindGroup
where Self: Send + Sync + 'static,

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

impl Component for ScreenSpaceAmbientOcclusionSettings
where Self: Send + Sync + 'static,

impl Component for ScreenSpaceAmbientOcclusionTextures
where Self: Send + Sync + 'static,

impl Component for ScreenSpaceReflectionsPipelineId
where Self: Send + Sync + 'static,

impl Component for ScreenSpaceReflectionsSettings
where Self: Send + Sync + 'static,

impl Component for ScreenSpaceReflectionsUniform
where Self: Send + Sync + 'static,

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

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

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

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

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

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

impl Component for ViewLightProbesUniformOffset
where Self: Send + Sync + 'static,

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

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

impl Component for ViewVolumetricFogPipeline
where Self: Send + Sync + 'static,

impl Component for ViewVolumetricFogUniformOffset
where Self: Send + Sync + 'static,

impl Component for VisibleClusterableObjects
where Self: Send + Sync + 'static,

impl Component for VolumetricFogSettings
where Self: Send + Sync + 'static,

impl Component for VolumetricLight
where Self: Send + Sync + 'static,

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

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

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

impl<C> Component for RenderViewLightProbes<C>
where C: LightProbeComponent, Self: Send + Sync + 'static,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl Component for GpuCulling
where Self: Send + Sync + 'static,

impl Component for NoCpuCulling
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

impl Component for VisibilityRange
where Self: Send + Sync + 'static,

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

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

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

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

impl Component for SceneInstanceReady
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

impl Component for NoWireframe2d
where Self: Send + Sync + 'static,

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

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

impl Component for SpriteSource
where Self: Send + Sync + 'static,

impl Component for SpriteViewBindGroup
where Self: Send + Sync + 'static,

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

impl Component for Wireframe2d
where Self: Send + Sync + 'static,

impl Component for Wireframe2dColor
where Self: Send + Sync + 'static,

impl<S: States> Component for StateTransitionEvent<S>
where Self: Send + Sync + 'static,

impl<S: States> Component for StateScoped<S>
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

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

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

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

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

impl Component for BorderRadius
where Self: Send + Sync + 'static,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl Component for AppLifecycle
where Self: Send + Sync + 'static,

impl Component for FileDragAndDrop
where Self: Send + Sync + 'static,

impl Component for Ime
where Self: Send + Sync + 'static,

impl Component for ClosingWindow
where Self: Send + Sync + 'static,

impl Component for CursorEntered
where Self: Send + Sync + 'static,

impl Component for CursorLeft
where Self: Send + Sync + 'static,

impl Component for CursorMoved
where Self: Send + Sync + 'static,

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

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

impl Component for RawHandleWrapperHolder
where Self: Send + Sync + 'static,

impl Component for ReceivedCharacter
where Self: Send + Sync + 'static,

impl Component for RequestRedraw
where Self: Send + Sync + 'static,

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

impl Component for WindowBackendScaleFactorChanged
where Self: Send + Sync + 'static,

impl Component for WindowCloseRequested
where Self: Send + Sync + 'static,

impl Component for WindowClosed
where Self: Send + Sync + 'static,

impl Component for WindowClosing
where Self: Send + Sync + 'static,

impl Component for WindowCreated
where Self: Send + Sync + 'static,

impl Component for WindowDestroyed
where Self: Send + Sync + 'static,

impl Component for WindowFocused
where Self: Send + Sync + 'static,

impl Component for WindowMoved
where Self: Send + Sync + 'static,

impl Component for WindowOccluded
where Self: Send + Sync + 'static,

impl Component for WindowResized
where Self: Send + Sync + 'static,

impl Component for WindowScaleFactorChanged
where Self: Send + Sync + 'static,

impl Component for WindowThemeChanged
where Self: Send + Sync + 'static,

impl Component for WinitEvent
where Self: Send + Sync + 'static,

impl Component for WakeUp
where Self: Send + Sync + 'static,

impl Component for CollideBox
where Self: Send + Sync + 'static,

impl Component for Acceleration
where Self: Send + Sync + 'static,

impl Component for Position
where Self: Send + Sync + 'static,

impl Component for Velocity
where Self: Send + Sync + 'static,

impl Component for Projectile
where Self: Send + Sync + 'static,

impl Component for BoundingBox
where Self: Send + Sync + 'static,

impl Component for BoundingBoxRoot
where Self: Send + Sync + 'static,

impl Component for Peashooter
where Self: Send + Sync + 'static,

impl Component for PeashooterHead
where Self: Send + Sync + 'static,

impl Component for GridPos
where Self: Send + Sync + 'static,

impl Component for SeedBank
where Self: Send + Sync + 'static,

impl Component for SeedPacketArea
where Self: Send + Sync + 'static,

impl Component for SeedPacketIndex
where Self: Send + Sync + 'static,

impl<B> Component for Periodic<B>
where Self: Send + Sync + 'static,

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

impl Component for Transform2D
where Self: Send + Sync + 'static,

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

impl Component for AutoNullTrigger
where Self: Send + Sync + 'static,

impl Component for CoolDown
where Self: Send + Sync + 'static,

impl Component for ModelState
where Self: Send + Sync + 'static,

impl Component for StateTransitionEvent
where Self: Send + Sync + 'static,

impl Component for TransitionTrigger
where Self: Send + Sync + 'static,