pub struct World { /* private fields */ }
Implementations§
Source§impl World
impl World
pub fn new() -> Self
Sourcepub fn add_resource(&mut self, resource_data: impl Any)
pub fn add_resource(&mut self, resource_data: impl Any)
Add a resource to the world so that anyone with access to the world can query for it immutably or mutably. Generally resources are pieces of data that are not associated with individual entities. An example of a resource could be the average fps.
Resources are stored based on their type id, so we could store one u32 resource. If we want to store more u32’s then we can wrap the data in a tuple struct. See the integration tests for an example.
use bbecs_tutorial::World;
let mut world = World::new();
world.add_resource(10_u32);
Sourcepub fn get_resource<T: Any>(&self) -> Option<&T>
pub fn get_resource<T: Any>(&self) -> Option<&T>
Query for a resource and get a reference to it. The type of the resource must be added in so that we can find it.
use bbecs_tutorial::World;
let mut world = World::new();
world.add_resource(10_u32);
let resource = world.get_resource::<u32>().unwrap();
assert_eq!(*resource, 10);
Sourcepub fn get_resource_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn get_resource_mut<T: Any>(&mut self) -> Option<&mut T>
Query for a resource and get a mutable reference to it. The type of the resource must be added in so that we can find it.
use bbecs_tutorial::World;
let mut world = World::new();
world.add_resource(10_u32);
{
let resource = world.get_resource_mut::<u32>().unwrap();
*resource += 1;
}
let resource = world.get_resource::<u32>().unwrap();
assert_eq!(*resource, 11);
Sourcepub fn delete_resource<T: Any>(&mut self)
pub fn delete_resource<T: Any>(&mut self)
This will remove the resource from the world, and it doesn’t care if the resource exists at this point in time.