EntityData

Struct EntityData 

Source
pub struct EntityData(/* private fields */);
Expand description

Holds all the data an entity contains, its components for example.

Implementations§

Source§

impl EntityData

Source

pub fn new() -> Self

Create a new EntityData

Source

pub fn add<C: Component>(&mut self, c: C)

Add a component to the entity

Examples found in repository?
examples/schedule.rs (line 36)
24fn add_bullets(world: &mut World) {
25    let bullets = [
26        (Position(4, 7), Velocity(3, 0)),
27        (Position(0, 0), Velocity(0, 0)),
28        (Position(5, -8), Velocity(0, -5)),
29        (Position(12, 0), Velocity(-1, 0)),
30    ];
31
32    for (position, velocity) in bullets {
33        let entity = world.spawn();
34        let entity = world.get_entity_mut(entity).unwrap();
35
36        entity.add(Bullet);
37        entity.add(position);
38        entity.add(velocity);
39    }
40}
More examples
Hide additional examples
examples/stress.rs (line 17)
6fn main() {
7    const ENTIIES: u64 = 1_000_000;
8    let time = SystemTime::now();
9
10    let mut world = World::new();
11
12    #[allow(dead_code)]
13    struct Number(u64);
14
15    for n in 0..=ENTIIES {
16        let id = world.spawn();
17        world.get_entity_mut(id).unwrap().add(Number(n));
18    }
19
20    for e in &Query::query_world(
21        &world,
22        QueryFilter::new()
23            .with_requires(&[component_id!(Number)])
24            .with_func(Some(|id, world| {
25                world.get_entity(id).unwrap().get::<Number>().unwrap().0 % 147 == 0
26            })),
27    ) {
28        world.despawn(e);
29    }
30
31    println!(
32        "spawning and filtering {ENTIIES} entities took {}ms",
33        time.elapsed().unwrap().as_millis()
34    );
35}
examples/basic.rs (line 22)
4fn main() {
5    let mut world = World::new();
6
7    struct Name(String);
8    struct Age(u8);
9    struct Human;
10
11    let people = [
12        (Name("Alice".to_owned()), Age(20)),
13        (Name("Bob".to_owned()), Age(26)),
14        (Name("Cathrine".to_owned()), Age(33)),
15        (Name("Dennis".to_owned()), Age(10)),
16    ];
17
18    for (name, age) in people {
19        let entity = world.spawn();
20        let entity = world.get_entity_mut(entity).unwrap();
21
22        entity.add(name);
23        entity.add(age);
24        entity.add(Human);
25    }
26
27    {
28        let entity = world.spawn();
29        let entity = world.get_entity_mut(entity).unwrap();
30
31        entity.add(Name("Jupiter".to_owned()));
32    }
33
34    for e in &Query::query_world(
35        &world,
36        QueryFilter {
37            requires: vec![
38                component_id!(Name),
39                component_id!(Age),
40                component_id!(Human),
41            ],
42            ..Default::default()
43        },
44    ) {
45        let e = world.get_entity_mut(e).unwrap();
46        let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47        let name = e.get_mut::<Name>().unwrap();
48
49        name.0 = name.0.to_uppercase();
50        println!("Found human named {}, who is {} years old", name.0, age.0);
51    }
52
53    for e in &Query::query_world(
54        &world,
55        QueryFilter {
56            requires: vec![component_id!(Name)],
57            ..Default::default()
58        },
59    ) {
60        let entity = world.get_entity(e).unwrap();
61        let name = entity.get::<Name>().unwrap();
62
63        println!("This entity (#{e}) has a name: {}", name.0);
64    }
65}
Source

pub fn get<C: Component>(&self) -> Option<&C>

Get a reference to the component of type C

§Examples
use xuko_ecs::prelude::*;

let mut world = World::new();
{
    let id = world.spawn();
    let entity_ref = world.get_entity(id).unwrap();
    if let Some(c) = entity_ref.get::<i32>() {
        // do stuff with `c`
    }
}
Examples found in repository?
examples/schedule.rs (line 54)
42fn move_entities(world: &mut World) {
43    let entities = Query::query_world(
44        world,
45        QueryFilter {
46            requires: vec![component_id!(Position), component_id!(Velocity)],
47            ..Default::default()
48        },
49    );
50
51    for e in &entities {
52        let entity = world.get_entity_mut(e).unwrap();
53
54        let velocity = entity.get::<Velocity>().cloned().unwrap();
55        let position = entity.get_mut::<Position>().unwrap();
56
57        position.0 += velocity.0;
58        position.1 += velocity.1;
59
60        println!("Something that has a position: {position:?}");
61    }
62}
More examples
Hide additional examples
examples/stress.rs (line 25)
6fn main() {
7    const ENTIIES: u64 = 1_000_000;
8    let time = SystemTime::now();
9
10    let mut world = World::new();
11
12    #[allow(dead_code)]
13    struct Number(u64);
14
15    for n in 0..=ENTIIES {
16        let id = world.spawn();
17        world.get_entity_mut(id).unwrap().add(Number(n));
18    }
19
20    for e in &Query::query_world(
21        &world,
22        QueryFilter::new()
23            .with_requires(&[component_id!(Number)])
24            .with_func(Some(|id, world| {
25                world.get_entity(id).unwrap().get::<Number>().unwrap().0 % 147 == 0
26            })),
27    ) {
28        world.despawn(e);
29    }
30
31    println!(
32        "spawning and filtering {ENTIIES} entities took {}ms",
33        time.elapsed().unwrap().as_millis()
34    );
35}
examples/basic.rs (line 46)
4fn main() {
5    let mut world = World::new();
6
7    struct Name(String);
8    struct Age(u8);
9    struct Human;
10
11    let people = [
12        (Name("Alice".to_owned()), Age(20)),
13        (Name("Bob".to_owned()), Age(26)),
14        (Name("Cathrine".to_owned()), Age(33)),
15        (Name("Dennis".to_owned()), Age(10)),
16    ];
17
18    for (name, age) in people {
19        let entity = world.spawn();
20        let entity = world.get_entity_mut(entity).unwrap();
21
22        entity.add(name);
23        entity.add(age);
24        entity.add(Human);
25    }
26
27    {
28        let entity = world.spawn();
29        let entity = world.get_entity_mut(entity).unwrap();
30
31        entity.add(Name("Jupiter".to_owned()));
32    }
33
34    for e in &Query::query_world(
35        &world,
36        QueryFilter {
37            requires: vec![
38                component_id!(Name),
39                component_id!(Age),
40                component_id!(Human),
41            ],
42            ..Default::default()
43        },
44    ) {
45        let e = world.get_entity_mut(e).unwrap();
46        let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47        let name = e.get_mut::<Name>().unwrap();
48
49        name.0 = name.0.to_uppercase();
50        println!("Found human named {}, who is {} years old", name.0, age.0);
51    }
52
53    for e in &Query::query_world(
54        &world,
55        QueryFilter {
56            requires: vec![component_id!(Name)],
57            ..Default::default()
58        },
59    ) {
60        let entity = world.get_entity(e).unwrap();
61        let name = entity.get::<Name>().unwrap();
62
63        println!("This entity (#{e}) has a name: {}", name.0);
64    }
65}
Source

pub fn get_mut<C: Component>(&mut self) -> Option<&mut C>

Get a mutable reference to the component of type C

Examples found in repository?
examples/schedule.rs (line 55)
42fn move_entities(world: &mut World) {
43    let entities = Query::query_world(
44        world,
45        QueryFilter {
46            requires: vec![component_id!(Position), component_id!(Velocity)],
47            ..Default::default()
48        },
49    );
50
51    for e in &entities {
52        let entity = world.get_entity_mut(e).unwrap();
53
54        let velocity = entity.get::<Velocity>().cloned().unwrap();
55        let position = entity.get_mut::<Position>().unwrap();
56
57        position.0 += velocity.0;
58        position.1 += velocity.1;
59
60        println!("Something that has a position: {position:?}");
61    }
62}
More examples
Hide additional examples
examples/basic.rs (line 47)
4fn main() {
5    let mut world = World::new();
6
7    struct Name(String);
8    struct Age(u8);
9    struct Human;
10
11    let people = [
12        (Name("Alice".to_owned()), Age(20)),
13        (Name("Bob".to_owned()), Age(26)),
14        (Name("Cathrine".to_owned()), Age(33)),
15        (Name("Dennis".to_owned()), Age(10)),
16    ];
17
18    for (name, age) in people {
19        let entity = world.spawn();
20        let entity = world.get_entity_mut(entity).unwrap();
21
22        entity.add(name);
23        entity.add(age);
24        entity.add(Human);
25    }
26
27    {
28        let entity = world.spawn();
29        let entity = world.get_entity_mut(entity).unwrap();
30
31        entity.add(Name("Jupiter".to_owned()));
32    }
33
34    for e in &Query::query_world(
35        &world,
36        QueryFilter {
37            requires: vec![
38                component_id!(Name),
39                component_id!(Age),
40                component_id!(Human),
41            ],
42            ..Default::default()
43        },
44    ) {
45        let e = world.get_entity_mut(e).unwrap();
46        let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47        let name = e.get_mut::<Name>().unwrap();
48
49        name.0 = name.0.to_uppercase();
50        println!("Found human named {}, who is {} years old", name.0, age.0);
51    }
52
53    for e in &Query::query_world(
54        &world,
55        QueryFilter {
56            requires: vec![component_id!(Name)],
57            ..Default::default()
58        },
59    ) {
60        let entity = world.get_entity(e).unwrap();
61        let name = entity.get::<Name>().unwrap();
62
63        println!("This entity (#{e}) has a name: {}", name.0);
64    }
65}
Source

pub fn keys(&self) -> Vec<ComponentId>

Get a list of all the types of components this entity has

Source

pub fn has_component<C: Component>(&self) -> bool

Check whether this map has component C

Source

pub fn remove<C: Component>(&mut self)

Remove a component from the entity

Trait Implementations§

Source§

impl Default for EntityData

Source§

fn default() -> EntityData

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,