bevy_ecs/world/
component_constants.rs

1//! Internal components used by bevy with a fixed component id.
2//! Constants are used to skip [`TypeId`] lookups in hot paths.
3use super::*;
4#[cfg(feature = "bevy_reflect")]
5use bevy_reflect::Reflect;
6
7/// [`ComponentId`] for [`OnAdd`]
8pub const ON_ADD: ComponentId = ComponentId::new(0);
9/// [`ComponentId`] for [`OnInsert`]
10pub const ON_INSERT: ComponentId = ComponentId::new(1);
11/// [`ComponentId`] for [`OnReplace`]
12pub const ON_REPLACE: ComponentId = ComponentId::new(2);
13/// [`ComponentId`] for [`OnRemove`]
14pub const ON_REMOVE: ComponentId = ComponentId::new(3);
15/// [`ComponentId`] for [`OnDespawn`]
16pub const ON_DESPAWN: ComponentId = ComponentId::new(4);
17
18/// Trigger emitted when a component is inserted onto an entity that does not already have that
19/// component. Runs before `OnInsert`.
20/// See [`crate::component::ComponentHooks::on_add`] for more information.
21#[derive(Event, Debug)]
22#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
23#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
24pub struct OnAdd;
25
26/// Trigger emitted when a component is inserted, regardless of whether or not the entity already
27/// had that component. Runs after `OnAdd`, if it ran.
28/// See [`crate::component::ComponentHooks::on_insert`] for more information.
29#[derive(Event, Debug)]
30#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
31#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
32pub struct OnInsert;
33
34/// Trigger emitted when a component is inserted onto an entity that already has that component.
35/// Runs before the value is replaced, so you can still access the original component data.
36/// See [`crate::component::ComponentHooks::on_replace`] for more information.
37#[derive(Event, Debug)]
38#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
39#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
40pub struct OnReplace;
41
42/// Trigger emitted when a component is removed from an entity, and runs before the component is
43/// removed, so you can still access the component data.
44/// See [`crate::component::ComponentHooks::on_remove`] for more information.
45#[derive(Event, Debug)]
46#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
47#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
48pub struct OnRemove;
49
50/// Trigger emitted for each component on an entity when it is despawned.
51/// See [`crate::component::ComponentHooks::on_despawn`] for more information.
52#[derive(Event, Debug)]
53#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
54#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
55pub struct OnDespawn;