Initialize a new binary crate using cargo init
. With an argument, cargo init will create a new directory for us.
cargo init boxes
cd boxes
Add bevy as a dependency using either cargo add or editing Cargo.toml. We’ll be using Bevy 0.10 for this workshop.
cargo add bevy@0.10
After adding bevy to the project, your Cargo.toml
should look like this:
[package]
name = "boxes"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = "0.10"
Bring the bevy prelude into scope in src/main.rs
at the top of the file.
use bevy::prelude::*;
A prelude is a common term for "things the crate author thinks you'll need most of the time when using their crate". Instead of manually bringing everything we want into scope using it's own module path, which can get tiring, we can bring everything from the prelude into scope using *
. Since a prelude is usually only re-exporting functions from the rest of the crate, this gives us easier access to what we would've needed anyway.
Bevy games are called "App"s and are constructed using a builder pattern. In this case we use App::new() to kick off the builder, then we add the [DefaultPlugins](https://docs.rs/bevy/0.10.0/bevy/struct.DefaultPlugins.html)
from the bevy prelude, and finally [.run()](https://docs.rs/bevy/0.10.0/bevy/app/struct.App.html#method.run)
executes the application.
fn main() {
App::new().add_plugins(DefaultPlugins).run()
}
Bevy’s core by itself doesn't do much because it allows us to add all of the functionality we want via plugins. These plugins can come from the project itself or third party crates.
DefaultPlugins
is a group of plugins that handle core bevy functionality such as enabling the window holding our game to show, handling user keyboard input, or diagnostics and logging.
You could imagine scenarios where we wouldn't want to render a window, such as if we were running bevy on a server in a multiplayer game.
Running this program shows us a window with the title Bevy App
and no content. Seeing this window means everything is set up appropriately.