Because the DefaultPlugins
include so much commonly used functionality, there is a convenience function for modifying the plugins contained in DefaultPlugins
.
To set the title of the window for our game, we can modify the WindowPlugin
using the .set()
function on DefaultPlugins
, and pass in our own WindowPlugin
to use.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "2048".to_string(),
..default()
}),
..default()
}))
.run()
}
When we say a struct is “A Plugin” what we mean is that the struct implements the Plugin
trait.
So WindowPlugin
is a regular struct that implements the Plugin trait, and it uses the values we set on the struct to do whatever it needs to do as a plugin.
In this case we only care about configuring the primary_window, which is the default window that shows up when we start a Bevy App.
The primary_window
’s value is an Option<Window>
because we could choose to not spawn a window as well, for example if we were running this application on a server.
So the value is either going to be None
or what we already have in our code.
There are 19 fields in the Window
struct but we only care about one: title
.
title
is a String
, so we know that Window
wants to own the data we use as a title. To do this we can use a string literal and call to_string()
on it.
Default values
So we have two structs that we’re building here: WindowPlugin
and Window
. In both cases we only care about one of the fields and the rest can be Bevy’s defaults for those values.
Most structs in Bevy implement the Default
trait, which means we can construct the default values for those structs using Default::default()
or Window::default()
.
Bevy also offers an ergonomic improvement to using this trait in that it exports default()
in the prelude that we brought into scope.
So combining all that, we can define the fields we want to change and spread the rest of the default values in using ..default()
.
Running the application now will show a window with a new title.
cargo run