The World is the central container for all ECS data. It stores Entities, Components, and Resources.
- World: Use
World::new() to create a container. It provides methods to spawn entities, insert_resource, and access data. - Resources: Global, unique data that does not belong to any specific entity (e.g.,
Time, AssetServer). Resources are identified by their type. You can access them in systems using the Res<T> or ResMut<T> parameter types.
use bevy_ecs::prelude::*
use bevy_ecs::world::World;
#[derive(Resource, Default)]
struct Time {
seconds: f32,
}
fn main() {
let mut world = World::new();
// Inserting a resource
world.insert_resource(Time::default());
// Accessing a resource from the world
let time = world.get_resource::<Time>().unwrap();
}
// Accessing a resource from a system
fn print_time(time: Res<Time>) {
println!("{}", time.seconds);
}