# Bevy ECS [![Crates.io](https://img.shields.io/crates/v/bevy_ecs.svg)](https://crates.io/crates/bevy_ecs) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/bevyengine/bevy/blob/HEAD/LICENSE) [![Discord](https://img.shields.io/discord/691052431525675048.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/bevy) ## What is Bevy ECS? Bevy ECS is an Entity Component System custom-built for the [Bevy][bevy] game engine. It aims to be simple to use, ergonomic, fast, massively parallel, opinionated, and featureful. It was created specifically for Bevy's needs, but it can easily be used as a standalone crate in other projects. ## ECS All app logic in Bevy uses the Entity Component System paradigm, which is often shortened to ECS. ECS is a software pattern that involves breaking your program up into Entities, Components, and Systems. Entities are unique "things" that are assigned groups of Components, which are then processed using Systems. For example, one entity might have a `Position` and `Velocity` component, whereas another entity might have a `Position` and `UI` component. You might have a movement system that runs on all entities with a Position and Velocity component. The ECS pattern encourages clean, decoupled designs by forcing you to break up your app data and logic into its core components. It also helps make your code faster by optimizing memory access patterns and making parallelism easier. ## Concepts Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS implementations, which often require complex lifetimes, traits, builder patterns, or macros, Bevy ECS uses normal Rust data types for all of these concepts: ### Components Components are normal Rust structs. They are data stored in a `World` and specific instances of Components correlate to Entities. ```rust use bevy_ecs::prelude::*; #[derive(Component)] struct Position { x: f32, y: f32 } ``` ### Worlds Entities, Components, and Resources are stored in a `World`. Worlds, much like Rust std collections like HashSet and Vec, expose operations to insert, read, write, and remove the data they store. ```rust use bevy_ecs::world::World; let world = World::default(); ``` ### Entities Entities are unique identifiers that correlate to zero or more Components. ```rust use bevy_ecs::prelude::*; #[derive(Component)] struct Position { x: f32, y: f32 } #[derive(Component)] struct Velocity { x: f32, y: f32 } let mut world = World::new(); let entity = world.spawn() .insert(Position { x: 0.0, y: 0.0 }) .insert(Velocity { x: 1.0, y: 0.0 }) .id(); let entity_ref = world.entity(entity); let position = entity_ref.get::().unwrap(); let velocity = entity_ref.get::().unwrap(); ``` ### Systems Systems are normal Rust functions. Thanks to the Rust type system, Bevy ECS can use function parameter types to determine what data needs to be sent to the system. It also uses this "data access" information to determine what Systems can run in parallel with each other. ```rust use bevy_ecs::prelude::*; #[derive(Component)] struct Position { x: f32, y: f32 } fn print_position(query: Query<(Entity, &Position)>) { for (entity, position) in query.iter() { println!("Entity {:?} is at position: x {}, y {}", entity, position.x, position.y); } } ``` ### Resources Apps often require unique resources, such as asset collections, renderers, audio servers, time, etc. Bevy ECS makes this pattern a first class citizen. `Resource` is a special kind of component that does not belong to any entity. Instead, it is identified uniquely by its type: ```rust use bevy_ecs::prelude::*; #[derive(Default)] struct Time { seconds: f32, } let mut world = World::new(); world.insert_resource(Time::default()); let time = world.get_resource::