Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add readme as docs to relevant crates. #2575

Closed
wants to merge 11 commits into from
92 changes: 72 additions & 20 deletions crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS impl
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 }
```

Expand All @@ -33,6 +36,8 @@ struct Position { x: f32, y: f32 }
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();
```

Expand All @@ -41,6 +46,15 @@ let world = World::default();
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 })
Expand All @@ -56,6 +70,11 @@ let velocity = entity_ref.get::<Velocity>().unwrap();
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);
Expand All @@ -68,18 +87,20 @@ fn print_position(query: Query<(Entity, &Position)>) {
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I object to using a tuple struct here. The named field is way better at documenting intent. Tuple structs rarely have a place in public apis imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me 👍

}
struct Time(f32);

let mut world = World::new();

world.insert_resource(Time::default());

let time = world.get_resource::<Time>().unwrap();

// You can also access resources from Systems
fn print_time(time: Res<Time>) {
println!("{}", time.seconds);
println!("{}", time.0);
}
```

Expand All @@ -98,18 +119,13 @@ Bevy ECS should feel very natural for those familiar with Rust syntax:
```rust
use bevy_ecs::prelude::*;

struct Velocity {
x: f32,
y: f32,
}

struct Position {
x: f32,
y: f32,
}
#[derive(Component)]
struct Position { x: f32, y: f32 }
#[derive(Component)]
struct Velocity { x: f32, y: f32 }

// This system moves each entity with a Position and Velocity component
fn movement(query: Query<(&mut Position, &Velocity)>) {
fn movement(mut query: Query<(&mut Position, &Velocity)>) {
for (mut position, velocity) in query.iter_mut() {
position.x += velocity.x;
position.y += velocity.y;
Expand Down Expand Up @@ -144,8 +160,18 @@ fn main() {
### Query Filters

```rust
// Gets the Position component of all Entities with Player component and without the RedTeam component
fn system(query: Query<&Position, (With<Player>, Without<RedTeam>)>) {
use bevy_ecs::prelude::*;

#[derive(Component)]
struct Position { x: f32, y: f32 }
#[derive(Component)]
struct Player;
#[derive(Component)]
struct Alive;

// Gets the Position component of all Entities with Player component and without the Alive
// component.
fn system(query: Query<&Position, (With<Player>, Without<Alive>)>) {
for position in query.iter() {
}
}
Expand All @@ -158,14 +184,21 @@ Bevy ECS tracks _all_ changes to Components and Resources.
Queries can filter for changed Components:

```rust
use bevy_ecs::prelude::*;

#[derive(Component)]
struct Position { x: f32, y: f32 }
#[derive(Component)]
struct Velocity { x: f32, y: f32 }

// Gets the Position component of all Entities whose Velocity has changed since the last run of the System
fn system(query: Query<&Position, Changed<Velocity>>) {
fn system_changed(query: Query<&Position, Changed<Velocity>>) {
for position in query.iter() {
}
}

// Gets the Position component of all Entities that had a Velocity component added since the last run of the System
fn system(query: Query<&Position, Added<Velocity>>) {
// Gets the i32 component of all Entities that had a f32 component added since the last run of the System
fn system_added(query: Query<&Position, Added<Velocity>>) {
for position in query.iter() {
}
}
Expand All @@ -174,6 +207,10 @@ fn system(query: Query<&Position, Added<Velocity>>) {
Resources also expose change state:

```rust
use bevy_ecs::prelude::*;

struct Time(f32);

// Prints "time changed!" if the Time resource has changed since the last run of the System
fn system(time: Res<Time>) {
if time.is_changed() {
Expand All @@ -195,7 +232,9 @@ Components can be stored in:

Component storage types are configurable, and they default to table storage if the storage is not manually defined.

```rs
```rust
use bevy_ecs::prelude::*;

#[derive(Component)]
struct TableStoredComponent;

Expand All @@ -209,13 +248,24 @@ struct SparseStoredComponent;
Define sets of Components that should be added together.

```rust
use bevy_ecs::prelude::*;

#[derive(Default, Component)]
struct Player;
#[derive(Default, Component)]
struct Position { x: f32, y: f32 }
#[derive(Default, Component)]
struct Velocity { x: f32, y: f32 }

#[derive(Bundle, Default)]
struct PlayerBundle {
player: Player,
position: Position,
velocity: Velocity,
}

let mut world = World::new();

// Spawn a new entity and insert the default PlayerBundle
world.spawn().insert_bundle(PlayerBundle::default());

Expand All @@ -231,6 +281,8 @@ world.spawn().insert_bundle(PlayerBundle {
Events offer a communication channel between one or more systems. Events can be sent using the system parameter `EventWriter` and received with `EventReader`.

```rust
use bevy_ecs::prelude::*;

struct MyEvent {
message: String,
}
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

pub mod archetype;
pub mod bundle;
pub mod change_detection;
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_reflect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This crate enables you to dynamically interact with Rust types:

### Derive the Reflect traits

```rust
```rust ignore
// this will automatically implement the Reflect trait and the Struct trait (because the type is a struct)
#[derive(Reflect)]
struct Foo {
Expand Down Expand Up @@ -44,7 +44,7 @@ let mut foo = Foo {

### Interact with fields using their names

```rust
```rust ignore
assert_eq!(*foo.get_field::<u32>("a").unwrap(), 1);

*foo.get_field_mut::<u32>("a").unwrap() = 2;
Expand All @@ -54,7 +54,7 @@ assert_eq!(foo.a, 2);

### "Patch" your types with new values

```rust
```rust ignore
let mut dynamic_struct = DynamicStruct::default();
dynamic_struct.insert("a", 42u32);
dynamic_struct.insert("c", vec![3, 4, 5]);
Expand All @@ -67,14 +67,14 @@ assert_eq!(foo.c, vec![3, 4, 5]);

### Look up nested fields using "path strings"

```rust
```rust ignore
let value = *foo.get_path::<f32>("d[0].value").unwrap();
assert_eq!(value, 3.14);
```

### Iterate over struct fields

```rust
```rust ignore
for (i, value: &Reflect) in foo.iter_fields().enumerate() {
let field_name = foo.name_at(i).unwrap();
if let Ok(value) = value.downcast_ref::<u32>() {
Expand All @@ -85,7 +85,7 @@ for (i, value: &Reflect) in foo.iter_fields().enumerate() {

### Automatically serialize and deserialize via Serde (without explicit serde impls)

```rust
```rust ignore
let mut registry = TypeRegistry::default();
registry.register::<u32>();
registry.register::<i32>();
Expand All @@ -109,7 +109,7 @@ assert!(foo.reflect_partial_eq(&dynamic_struct).unwrap());

Call a trait on a given &dyn Reflect reference without knowing the underlying type!

```rust
```rust ignore
#[derive(Reflect)]
#[reflect(DoThing)]
struct MyType {
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

mod list;
mod map;
mod path;
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_tasks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

mod slice;
pub use slice::{ParallelSlice, ParallelSliceMut};

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

pub mod components;
pub mod hierarchy;
pub mod transform_propagate_system;
Expand Down