Struct amethyst_core::Named

source ·
pub struct Named {
    pub name: Cow<'static, str>,
}
Expand description

A component that gives a name to an Entity.

There are two ways you can get a name for an entity:

  • Hard-coding the entity name in code, in which case the name would be a &'static str.
  • Dynamically generating the string or loading it from a data file, in which case the name would be a String.

To support both of these cases smoothly, Named stores the name as Cow<'static, str>. You can pass either a &'static str or a String to Named::new, and your code can generally treat the name field as a &str without needing to know whether the name is actually an owned or borrowed string.

Examples

Creating a name from string constant:

use amethyst::core::{Named, WithNamed};
use amethyst::ecs::prelude::*;

let mut world = World::new();
world.register::<Named>();

world
    .create_entity()
    .named("Super Cool Entity")
    .build();

Creating a name from a dynamically generated string:

use amethyst::core::{Named, WithNamed};
use amethyst::ecs::prelude::*;

let mut world = World::new();
world.register::<Named>();

for entity_num in 0..10 {
    world
        .create_entity()
        .named(format!("Entity Number {}", entity_num))
        .build();
}

Accessing a named entity in a system:

use amethyst::core::Named;
use amethyst::ecs::prelude::*;

pub struct NameSystem;

impl<'s> System<'s> for NameSystem {
    type SystemData = (
        Entities<'s>,
        ReadStorage<'s, Named>,
    );

    fn run(&mut self, (entities, names): Self::SystemData) {
        for (entity, name) in (&*entities, &names).join() {
            println!("Entity {:?} is named {}", entity, name.name);
        }
    }
}

Fields

name: Cow<'static, str>

The name of the entity this component is attached to.

Implementations

Constructs a new Named from a string.

Examples

From a string constant:

use amethyst::core::Named;

let name_component = Named::new("Super Cool Entity");

From a dynamic string:

use amethyst::core::Named;

let entity_num = 7;
let name_component = Named::new(format!("Entity Number {}", entity_num));

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Associated storage type for this component.
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.