Struct amethyst_core::Named
source · 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
sourceimpl Named
impl Named
sourcepub fn new<S>(name: S) -> Selfwhere
S: Into<Cow<'static, str>>,
pub fn new<S>(name: S) -> Selfwhere
S: Into<Cow<'static, str>>,
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
sourceimpl Component for Named
impl Component for Named
type Storage = DenseVecStorage<Named>
type Storage = DenseVecStorage<Named>
Associated storage type for this component.
sourceimpl<'de> Deserialize<'de> for Named
impl<'de> Deserialize<'de> for Named
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations
impl RefUnwindSafe for Named
impl Send for Named
impl Sync for Named
impl Unpin for Named
impl UnwindSafe for Named
Blanket Implementations
impl<T> Any for Twhere
T: Any,
impl<T> Any for Twhere
T: Any,
fn get_type_id(&self) -> TypeId
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Pointable for T
impl<T> Pointable for T
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read morefn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self
is actually part of its subset T
(and can be converted to it).unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.