I am learning Rust and Bevy engine, and I want to keep certain assets (such as Font
s) loaded during the entire application life-time.
// Resource for fonts:
#[derive(Default, Clone)]
pub struct ResFont {
pub ui: Handle<Font>, // The font that will be used in this example.
pub dialog: Handle<Font>,
...
}
// Insert resource in main() during App building:
{
.insert_resource(ResFont::default())
}
// Load resource during startup:
pub fn startup(asset_server: Res<AssetServer>, mut res_font: ResMut<ResFont>)
{
res_font.ui = asset_server.load("font/Default.ttf");
}
// Use the font resource in a different place:
pub fn setup_ui(res_font: ResMut<ResFont>)
{
...
TextStyle {
font: res_font.ui.clone(),
font_size: 12.0,
color: Color::WHITE,
}
...
}
In the function setup_ui()
at the bottom, I am using .clone()
to copy that asset. If I don't use .clone()
, I get the error:
cannot move out of dereference of `bevy::prelude::ResMut<'_, resource::text::ResFont>`
move occurs because value has type `bevy::prelude::Handle<bevy::prelude::Font>`, which does not implement the `Copy` traitrustc(E0507)
ui.rs(19, 27): move occurs because value has type `bevy::prelude::Handle<bevy::prelude::Font>`, which does not implement the `Copy` trait
I have two questions:
Am I copying the entire
Font
here during the.clone()
operation?Is this the "proper" way to keep resources loaded and for use later, or is there is a better way to achieve this that I don't know of?