Skip to content

Correct approach to adding component to entity returned from another function? #5868

Answered by afonsolage
njelly asked this question in Q&A
Discussion options

You must be logged in to vote

You can pass Commands a mutable borrowed:

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);
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by njelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants