Macro gecs::ecs_component_id
source · macro_rules! ecs_component_id {
{...} => { ... };
}
Expand description
Returns the compile-time ID of a given component in its archetype.
ecs_component_id!(Component); // Can be used in a query body
ecs_component_id!(Component, Archetype); // If used outside of a query
The ecs_component_id!
returns the compile-time ID (as a u8
) of a given component in
an archetype. If used in a query, the archetype parameter defaults to MatchedArchetype
,
which is the type alias automatically set for each query referencing the current matched
archetype for the current execution of the query body.
This is a const operation, and can be used to parameterize const generics.
By default, component IDs are assigned sequentially starting at 0
, with a maximum of
255
. Component IDs can also be manually set using the #[component_id(N)]
attribute
on elements of the component list in an ecs_archetype!
declaration within ecs_world!
.
Like enum discriminants, components without this attribute will count up from the last
manually set ID.
Example
use gecs::prelude::*;
pub struct CompA;
pub struct CompB;
pub struct CompC;
ecs_world! {
ecs_archetype!(
ArchFoo,
5,
CompA, // = 0
CompC, // = 1
);
ecs_archetype!(
ArchBar,
5,
#[component_id(6)]
CompA, // = 6
CompB, // = 7 (Implicit)
CompC, // = 8 (Implicit)
);
ecs_archetype!(
ArchBaz,
5,
CompA, // = 0 (Implicit)
CompB, // = 1 (Implicit)
#[component_id(200)]
CompC, // = 200
);
}
fn main() {
let mut world = EcsWorld::default();
let entity_a = world.archetype_mut::<ArchFoo>().create((CompA, CompC));
let entity_b = world.archetype_mut::<ArchBar>().create((CompA, CompB, CompC));
let entity_c = world.archetype_mut::<ArchBaz>().create((CompA, CompB, CompC));
ecs_find!(world, entity_a, |_: &CompC| {
assert_eq!(ecs_component_id!(CompC), 1);
});
ecs_find!(world, entity_b, |_: &CompC| {
assert_eq!(ecs_component_id!(CompC), 8);
});
ecs_find!(world, entity_c, |_: &CompC| {
assert_eq!(ecs_component_id!(CompC), 200);
});
assert_eq!(ecs_component_id!(CompC, ArchFoo), 1);
assert_eq!(ecs_component_id!(CompC, ArchBar), 8);
assert_eq!(ecs_component_id!(CompC, ArchBaz), 200);
}