Initialize required components with system parameters #18182
-
I'm curious what the ideal/idiomatic way of initializing components that require systems is. For context, I want consumers of my plugin to be able to spawn MyEntity and have the model (gltf) loaded without the consumer having to know which one to load. My current setup is this: use bevy::prelude::*;
#[derive(Component)]
#[require(SceneRoot)]
pub struct MyEntity;
pub struct MyEntityPlugin;
impl Plugin for MyEntityPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, setup_entity);
}
}
fn setup_entity(
mut commands: Commands,
asset_server: Res<AssetServer>,
query: Query<Entity, Added<MyEntity>>,
) {
let model = asset_server.load(GltfAssetLabel::Scene(0).from_asset("my_entity.glb"));
for entity in &query {
let mut entity = commands.entity(entity);
entity.insert(SceneRoot(model.clone()));
}
} This works, but I'm sure there's a simpler/safer/more effective way to do this. I've looked at different crates to do this, but I would have expected something like this to be possible: #[derive(Component)]
#[require(SceneRoot(load_model))]
pub struct MyEntity;
fn load_model(asset_server: Res<AssetServer>) -> SceneRoot {
SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset("my_entity.glb")))
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This flavor of behavior is likely to be handled with the |
Beta Was this translation helpful? Give feedback.
This flavor of behavior is likely to be handled with the
Construct
trait in the future, touched on in #14437. #18017 gives you some related functionality, and #18090 is an issue for a feature request that takesself
.