Skip to content

Short code improvements #208

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions content/learn/book/contributing/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,28 @@ A local server should start and you should be able to access a local version of

### Rust API Doc Syntax

We made an extension to the markdown syntax that makes linking to Rust API docs nicer. It also gives the links special formatting. Here are some examples:
We made an extension to the markdown syntax that makes linking to Rust API docs easier and prettier.
Here are some example links and the associated shortcodes:

* Full Type Path: {{rust_type(type="struct" crate="std", mod="collections", name="HashMap")}}
- {{rust_docs(type="struct" crate="std" mod="collections" name="HashMap")}}: ???. This is the standard invocation.
- {{rust_docs(type="struct" mod="ecs::system" name="Commands" method = "spawn")}}: ???. We can specify which method, field, variant or trait implementation we're referring to using the `method`, `field`,`variant` or `impl` arguments.
- {{rust_docs(type="trait" crate="bevy_ecs" mod="system" name="Command" method = "write")}}: ???. Change the `type` argument to link to enums, traits or keywords.
- {{rust_docs(type="struct" crate="std" mod="result" name="Result" show_mod=true)}}: ???. By using `show_mod`, we can see the full path.
- {{rust_docs(crate="std" mod="collections")}}: ???. By omitting `type` and `name`, we can link to the crate or module itself.

```{{curly_open()}}{rust_type(type="struct" crate="std" mod="collections" name="HashMap")}{{curly_close()}}```
* Short Type: {{rust_type(type="struct", crate="std" mod="collections", name="HashMap", no_mod=true)}}
By default, the `crate` argument is set to "bevy", as almost all of the linked docs are part of the Bevy umbrella.
For example, you can use `mod = "ecs"` or so to link to the items within the `bevy_ecs` crate correctly.

```{{curly_open()}}{rust_type(type="struct" crate="std" mod="collections" name="HashMap" no_mod=true)}{{curly_close()}}```
* Plural Type: {{rust_type(type="struct" crate="std" mod="collections" name="HashMap" no_mod=true plural=true)}}
There are several options available, toggled by adding the following arguments (separating each argument with a space) within the curly braces:

```{{curly_open()}}{rust_type(type="struct" crate="std" mod="collections" name="HashMap" no_mod=true, plural=true)}{{curly_close()}}```
* Function: {{rust_type(type="struct" crate="std" mod="collections" name="HashMap" no_mod=true method="insert")}}
- `show_crate`: shows the originating crate in the path
- `show_mod`: shows the originating module in the path
- `args`: adds the string as function arguments, wrapped in parentheses.
- `type_args`: adds the string as generic type arguments, wrapped in angle brackets
- `plural`: adds an "s" at the end of the link's name
- `possesive`: adds an "'s" as the end of the link's name

```{{curly_open()}}{rust_type(type="struct" crate="std" mod="collections" name="HashMap" no_mod=true method="insert")}{{curly_close()}}```
* Module: {{rust_mod(crate="std" mod="collections")}}

```{{curly_open()}}{rust_mod(crate="std" mod="collections")}{{curly_close()}}```

Modules from {{rust_mod(crate="std")}} will link to [https://doc.rust-lang.org](https://doc.rust-lang.org/std/index.html). Other modules (like {{rust_mod(crate="bevy_render" mod="render_graph")}} ) will link to [https://docs.rs](https://docs.rs).
Modules from {{rust_docs(crate="std")}} will link to [doc.rust-lang.org](https://doc.rust-lang.org/std/index.html). Other modules (like {{rust_docs(crate="bevy_render" mod="render_graph")}} ) will link to [docs.rs](https://docs.rs).

## Rust API Docs

Expand Down
2 changes: 1 addition & 1 deletion content/learn/book/getting-started/apps/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ template = "book-section.html"
page_template = "book-section.html"
+++

Bevy programs are referred to as {{rust_type(type="struct", crate="bevy_app", name="App", no_mod=true, plural=true)}}. The simplest Bevy app looks like this:
Bevy programs are referred to as {{rust_docs(type="struct", crate="bevy_app", name="App", plural=true)}}. The simplest Bevy app looks like this:

```rs
use bevy::prelude::*;
Expand Down
8 changes: 4 additions & 4 deletions content/learn/book/getting-started/ecs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ fn main() {
}
```

Note the `hello_world.system()` function call. This is a "trait extension method" that converts the `hello_world` function into the {{rust_type(type="trait" crate="bevy_ecs" mod="system" no_mod=true name="System")}} type.
Note the `hello_world.system()` function call. This is a "trait extension method" that converts the `hello_world` function into the {{rust_docs(type="trait" crate="bevy_ecs" mod="system" name="System")}} type.

The {{rust_type(type="struct" crate="bevy_app", name="AppBuilder" method="add_system" no_struct=true)}} function adds the system to your App's {{rust_type(type="struct", crate="bevy_ecs", mod="schedule" no_mod=true name="Schedule")}}, but we'll cover that more later.
The {{rust_docs(type="struct" crate="bevy_app", name="AppBuilder" method="add_system" no_struct=true)}} function adds the system to your App's {{rust_docs(type="struct", crate="bevy_ecs", mod="schedule" name="Schedule")}}, but we'll cover that more later.

Now run your App again using `cargo run`. You should see `hello world!` printed once in your terminal.

Expand All @@ -76,7 +76,7 @@ But what if we want our people to have a name? In a more traditional design, we
struct Name(String);
```

We can then add `People` to our {{rust_type(type="struct" crate="bevy_ecs" mod="world" no_mod=true name="World")}} using a "startup system". Startup systems are just like normal systems, but they run exactly once, before all other systems, right when our app starts. Let's use {{rust_type(type="struct" crate="bevy_ecs" mod="system" no_mod=true name="Commands")}} to spawn some entities into our {{rust_type(type="struct" crate="bevy_ecs" mod="world" no_mod=true name="World")}}:
We can then add `People` to our {{rust_docs(type="struct" crate="bevy_ecs" mod="world" name="World")}} using a "startup system". Startup systems are just like normal systems, but they run exactly once, before all other systems, right when our app starts. Let's use {{rust_docs(type="struct" crate="bevy_ecs" mod="system" name="Commands")}} to spawn some entities into our {{rust_docs(type="struct" crate="bevy_ecs" mod="world" name="World")}}:

```rs
fn add_people(mut commands: Commands) {
Expand All @@ -97,7 +97,7 @@ fn main() {
}
```

We could run this App now and the `add_people` system would run first, followed by `hello_world`. But our new people don't have anything to do yet! Let's make a system that properly greets the new citizens of our {{rust_type(type="struct" crate="bevy_ecs" mod="world" no_mod=true name="World")}}:
We could run this App now and the `add_people` system would run first, followed by `hello_world`. But our new people don't have anything to do yet! Let's make a system that properly greets the new citizens of our {{rust_docs(type="struct" crate="bevy_ecs" mod="world" name="World")}}:

```rs
fn greet_people(query: Query<&Name, With<Person>>) {
Expand Down
10 changes: 5 additions & 5 deletions content/learn/book/getting-started/plugins/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ template = "book-section.html"
page_template = "book-section.html"
+++

One of Bevy's core principles is modularity. All Bevy engine features are implemented as plugins. This includes internal features like the renderer, but games themselves are also implemented as plugins! This empowers developers to pick and choose which features they want. Don't need a UI? Don't register the {{rust_type(type="struct" crate="bevy_ui", name="UiPlugin")}}. Want to build a headless server? Don't register the {{rust_type(type="struct" crate="bevy_render" name="RenderPlugin")}}.
One of Bevy's core principles is modularity. All Bevy engine features are implemented as plugins. This includes internal features like the renderer, but games themselves are also implemented as plugins! This empowers developers to pick and choose which features they want. Don't need a UI? Don't register the {{rust_docs(type="struct" crate="bevy_ui", name="UiPlugin")}}. Want to build a headless server? Don't register the {{rust_docs(type="struct" crate="bevy_render" name="RenderPlugin")}}.

This also means you are free to replace any components you don't like. If you feel the need, you are welcome to build your own {{rust_type(type="struct" crate="bevy_ui" name="UiPlugin")}}, but consider [contributing it back to Bevy](/learn/book/contributing) if you think it would be useful!
This also means you are free to replace any components you don't like. If you feel the need, you are welcome to build your own {{rust_docs(type="struct" crate="bevy_ui" name="UiPlugin")}}, but consider [contributing it back to Bevy](/learn/book/contributing) if you think it would be useful!

However, most developers don't need a custom experience and just want the "full engine" experience with no hassle. For this, Bevy provides a set of "default plugins".

Expand All @@ -31,8 +31,8 @@ fn main() {
Once again run `cargo run`.

You should hopefully notice two things:
* **A window should pop up**. This is because we now have {{rust_type(type="struct" crate="bevy_window" name="WindowPlugin")}}, which defines the window interface (but doesn't actually know how to make windows), and {{rust_type(type="struct" crate="bevy_winit" name="WinitPlugin")}} which uses the [winit library](https://github.com/rust-windowing/winit) to create a window using your OS's native window api.
* **Your console is now full of "hello" messages**: This is because {{rust_type(type="struct" crate="bevy" name="DefaultPlugins")}} adds an "event loop" to our application. Our App's ECS Schedule now runs in a loop once per "frame". We will resolve the console spam in a moment.
* **A window should pop up**. This is because we now have {{rust_docs(type="struct" crate="bevy_window" name="WindowPlugin")}}, which defines the window interface (but doesn't actually know how to make windows), and {{rust_docs(type="struct" crate="bevy_winit" name="WinitPlugin")}} which uses the [winit library](https://github.com/rust-windowing/winit) to create a window using your OS's native window api.
* **Your console is now full of "hello" messages**: This is because {{rust_docs(type="struct" name="DefaultPlugins")}} adds an "event loop" to our application. Our App's ECS Schedule now runs in a loop once per "frame". We will resolve the console spam in a moment.

Note that `add_plugins(DefaultPlugins)` is equivalent to the following:
```rs
Expand All @@ -50,7 +50,7 @@ You are free to use whatever approach suits you!

## Creating your first plugin

For better organization, let's move all of our "hello" logic to a plugin. To create a plugin we just need to implement the {{rust_type(type="trait" name="Plugin" crate="bevy_app" no_mod=true)}} interface. Add the following code to your `main.rs` file:
For better organization, let's move all of our "hello" logic to a plugin. To create a plugin we just need to implement the {{rust_docs(type="trait" name="Plugin" crate="bevy_app")}} interface. Add the following code to your `main.rs` file:

```rs
pub struct HelloPlugin;
Expand Down
2 changes: 1 addition & 1 deletion content/learn/book/getting-started/resources/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here are some examples of data that could be encoded as **Resources**:

## Tracking Time with Resources

Let's solve our App's "hello spam" problem by only printing "hello" once every two seconds. We'll do this by using the {{rust_type(type="struct" crate="bevy_core" name="Time")}} resource, which is automatically added to our App via `add_plugins(DefaultPlugins)`.
Let's solve our App's "hello spam" problem by only printing "hello" once every two seconds. We'll do this by using the {{rust_docs(type="struct" crate="bevy_core" name="Time")}} resource, which is automatically added to our App via `add_plugins(DefaultPlugins)`.

For simplicity, remove the `hello_world` system from your App. This way we only need to adapt the `greet_people` system.

Expand Down
10 changes: 5 additions & 5 deletions content/learn/book/migration-guides/0.4-0.5/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ fn foo(mut commands: Commands) {

Systems using the old `commands: &mut Commands` syntax in 0.5 will fail to compile when calling `foo.system()`.

This change was made because {{rust_type(type="struct" crate="bevy_ecs" mod="system" version="0.5.0" name="Commands" no_mod=true)}}
now holds an internal {{rust_type(type="struct" crate="bevy_ecs" mod="world" version="0.5.0" name="World" no_mod=true)}}
This change was made because {{rust_docs(type="struct" crate="bevy_ecs" mod="system" version="0.5.0" name="Commands")}}
now holds an internal {{rust_docs(type="struct" crate="bevy_ecs" mod="world" version="0.5.0" name="World")}}
reference to enable safe entity allocations.

Note: The internal {{rust_type(type="struct" crate="bevy_ecs" mod="world" version="0.5.0" name="World" no_mod=true)}} reference requires two lifetime parameters to pass Commands into a non-system function: `commands: &'a mut Commands<'b>`
Note: The internal {{rust_docs(type="struct" crate="bevy_ecs" mod="world" version="0.5.0" name="World")}} reference requires two lifetime parameters to pass Commands into a non-system function: `commands: &'a mut Commands<'b>`

### Commands API

The {{rust_type(type="struct" crate="bevy_ecs" version="0.5.0" mod="system" name="Commands" no_mod=true)}} API has been completely reworked for consistency with the {{rust_type(type="struct" crate="bevy_ecs" mod="world" version="0.5.0" name="World" no_mod=true)}} API.
The {{rust_docs(type="struct" crate="bevy_ecs" version="0.5.0" mod="system" name="Commands")}} API has been completely reworked for consistency with the {{rust_docs(type="struct" crate="bevy_ecs" mod="world" version="0.5.0" name="World")}} API.

```rust
// 0.4
Expand Down Expand Up @@ -97,7 +97,7 @@ if timer.tick(time.delta()).finished() { /* do stuff */ }
timer.elapsed() // returns a `Duration`
```

Most of the methods of {{rust_type(type="struct" crate="bevy_core" version="0.5.0" name="Timer" no_mod=true)}}
Most of the methods of {{rust_docs(type="struct" crate="bevy_core" version="0.5.0" name="Timer")}}
now use `Duration` instead of `f32`.

This change allows timers to have consistent, high precision. For convenience, there is also an
Expand Down
Loading