Correct approach to adding component to entity returned from another function? #5868
-
I am trying to create a workflow where I can spawn an entity with a base set of components (a sprite sheet bundle, an animation component, some other custom components, etc.). Most entities in the game will use this common set. However, I want to occasionally spawn that entity, and then add onto it a I can create a spawn_unit function that takes in mut commands as a param, call it from my spawn_player function, but in my spawn_player function I am unable to add my
If I try another solution using
I can't do this because of this error, which implies it isn't possible to have nested bundles: What's the correct approach here? I don't want to copy and paste the same code in multiple places just to add one more additional component for a special case (my player unit). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
To have nested bundles you need to add ‘#[bundle]’ above ‘sprite_sheet_bundle’ in the second approach |
Beta Was this translation helpful? Give feedback.
-
You can pass fn spawn(commands: &mut Commands, sprite_sheet: Res<SpriteSheet>, config UnitConfig) {
// Do the thing!
}
fn spawn_player(mut commands: Commands, sprite_sheet: Res<SpriteSheet>) {
let playerEntity = unit::spawn(
&mut commands, // now borrow checker won't yell at you anymore
sprite_sheet,
UnitConfig {
facing: CardinalDirection4::East,
position: Vec2::splat(0.),
move_speed: 3.,
name: "Player".to_string(),
sprite_indexes: vec![0, 1],
sprite_frame_duration: vec![1., 1.],
},
);
commands.entity(playerEntity).insert(Player);
} |
Beta Was this translation helpful? Give feedback.
You can pass
Commands
a mutable borrowed: