pub struct Resources { /* private fields */ }Expand description
Stores ‘singleton’ data values in the ECS.
A struct storing a hashmap of type id and value pairs. It is used as a resource storage in the ecs.
Implementations§
Source§impl Resources
impl Resources
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates and returns a new Resources struct using its Default Implementation.
use sceller::prelude::*;
struct Health(u8);
let mut resources = Resources::new();Sourcepub fn add<T: Any>(&mut self, res: T)
pub fn add<T: Any>(&mut self, res: T)
Inserts any value implementing the std::any::Any trait into the instance of the Resources struct provided.
use sceller::prelude::*;
struct Health(u8);
let mut resources = Resources::new();
resources.add(Health(10));
assert_eq!(
resources.get_ref::<Health>().unwrap().0,
10
);Sourcepub fn get_ref<T: Any>(&self) -> Result<Ref<'_, T>>
pub fn get_ref<T: Any>(&self) -> Result<Ref<'_, T>>
Gets and optionally returns an immutable reference any given resource from the Resources struct provided.
Makes use of turbofish syntax (::
Note: This function internally uses downcast_ref()
use sceller::prelude::*;
struct Health(f32);
let mut resources = Resources::new();
resources.add(Health(42.0));
let extracted_health = resources.get_ref::<Health>().unwrap();
assert_eq!(extracted_health.0, 42.0);Sourcepub fn get_mut<T: Any>(&self) -> Result<RefMut<'_, T>>
pub fn get_mut<T: Any>(&self) -> Result<RefMut<'_, T>>
Optionally returns a mutable reference to a value of the given type.
use sceller::prelude::*;
#[derive(Debug, PartialEq)]
struct Health(i32);
let mut resources = Resources::new();
resources.add(Health(123));
{
let mut hp = resources.get_mut::<Health>().unwrap();
assert_eq!(hp.0, 123);
hp.0 = 42;
}
let hp = resources.get_ref::<Health>().unwrap();
assert_eq!(hp.0, 42);Sourcepub fn delete<T: Any>(&mut self) -> Result<T>
pub fn delete<T: Any>(&mut self) -> Result<T>
Attempts to delete and return a resource.
use sceller::prelude::*;
#[derive(Debug, PartialEq)]
struct Health(i32);
let mut resources = Resources::new();
resources.add(Health(123));
{
let hp = resources.get_ref::<Health>().unwrap();
assert_eq!(hp.0, 123);
}
resources.delete::<Health>().unwrap();
assert!(resources.get_ref::<Health>().is_err());This function tries to return the value that was stored in the Resources struct, and returns None if the type doesn’t exist;
use sceller::prelude::*;
#[derive(Debug, PartialEq)]
struct Health(i32);
struct No;
let mut resources = Resources::new();
resources.add(Health(123));
{
let hp = resources.get_ref::<Health>().unwrap();
assert_eq!(hp.0, 123);
}
{
let res = resources.delete::<Health>();
assert!(res.is_ok());
}
let res = resources.delete::<No>();
assert!(!res.is_ok());